/* CSC 17 Lab 1 due BEFORE next week's lab The first assignment asks you to convert a C++ program into Java, then make some additions to the java program. You are expected to also have read the C++ to Java tutorial. Type "javac progname.java" to compile source code progname.java. Then type "java progname" to run the program. Remember: if the file name is "progname.java", then the public class that contains main must be called "progname" as well. 1. The program you're asked to convert implements "Time" objects with a minute and a second value. For example, 'new Time(2,15)' creates a heap-allocated object representing 2 minutes and 15 seconds. You will then implement a number of operations including adding two time values (2min 15sec plus 1min 50sec = 4min 5sec). The C++ program was written in a way so that it can be easily converted to a close java equivalent. For example, it only uses heap-allocated objects with pointers, though I didn't use smart pointers as most of you are not familiar with those. I also marked each method "virtual" because technically there would be no java equivalent otherwise. To have std::cout call the "toString()" method in C++, you would have to overload the '<<' operator in C++. In java however, by the fact that functions are virtual by default (without using the keyword), when System.out.println (or printf) prints a time object, it would automatically look for a "toString" method. You can't do operator overloading in Java. Also, when writing the toString method in Java, you won't need to call a method to convert a number to a string: Java does that automatically because every value in Java have some 'toString' method. When you say minutes + "min", it automatically converts minutes to a string. INSTEAD OF TRYING TO EDIT THE C++ PROGRAM, YOU SHOULD TYPE IN A JAVA PROGRAM FROM SCRATCH. */ /* C++ program for managing gas stations: */ #include #include using namespace std; class Time { protected: // protected means values visible in time class and in subclasses int minutes; int seconds; public: Time(int m, int s) { int extra_minutes = s/60; seconds = s%60; minutes = m+extra_minutes; }//constructor Time() { // alternate constructor (also allowed in java) minutes = seconds = 0; }//alternate constructor. virtual int total_seconds() { return minutes*60 + seconds; } // convert Time object to printable string form: // std::to_string is available in C++ 2011 or later virtual string toString() { return to_string(minutes) + " min " + to_string(seconds) + " sec"; }// toString // increment this Time object by one second destructively (changes existing // object) virtual void tick() { seconds++; minutes += seconds/60; seconds = seconds%60; }//tick // compare this Time value with other Time value, return 0 if times are // equal, negative value if this time is less than other time and // positive value if this time is greater than other time. For example, // 1 min 30 seconds is less than 2 minutes and 10 seconds virtual int compareTo(Time* other) { int total1 = this->total_seconds(); int total2 = other->total_seconds(); return total1 - total2; }// compareTo // add a pair of Time values non-destructively (produces a new object, // does not change any existing object). Take pointer to other Time object // and returns pointer to newly heap-allocated Time object virtual Time* add(Time* other) { int s = seconds + other->seconds; int m = minutes + other->minutes + s/60; s = s%60; return new Time(m,s); }//sum }; //Time class // overload "<<" on cout so printing will auto-invoke toString method ostream& operator <<(ostream& outstream, Time* t) { outstream << t->toString(); return outstream; } int main(int argc, char* argv[]) { Time* t1 = new Time(2,15); t1->tick(); Time* t2 = new Time(1,44); Time* t3 = t1->add(t2); cout << "t1: " << t1 << endl; cout << "t2: " << t2 << endl; cout << "sum Time: " << t3 << endl; cout << "compare t1 and t2: " << t1->compareTo(t2) << endl; cout << "compare t1 with equivalent value: "<< t1->compareTo(new Time(0,136)); return 0; }//main /* Part 2. Modify the Java program by adding an "hour" value to the Time class and change all methods accordingly, and append main to demonstrate. Part 2 Challenge (optional). Implement the "hour" modification using *inheritance*. That is, do not change any existing code. Rather, define a subclass of Time that extends Time and overrides some methods. In C++ this can be done with: */ class HTime : public Time // public inheritance is by default in Java { protected: int hours; public: HTime(int h, int m, int s):Time(m,s) { int extra_hours = minutes/60; minutes = minutes%60; hours = h + extra_hours; }// subclass constructor. // in Java, to call the superclass constructor, call 'super(m,s);' inside // the body of the constructor. int total_seconds() override { return hours*3600 + minutes*60 + seconds; }// new version of total_seconds string toString() override { return to_string(hours)+"hrs " + Time::toString(); }// toString // in Java, to call the superclass version of method, call super.toString() void tick() override { this->Time::tick(); hours += minutes/60; minutes = minutes%60; }//tick HTime* add(Time* other) override { int other_seconds = other->total_seconds(); int s = seconds + other_seconds; int m = minutes + seconds/60; int h = hours + m/60; s = s%60; m = m%60; return new HTime(h,m,s); }//sum };// HTime subclass of Time