Jump to content

So far this is what i got and i just want some opinions on it. Does it look good or bad?

public class Clock {                private double minutes;                private double hours;                                public Clock(){                minutes = 0;                hours = 0;                }                public Clock(double minutes, double hours){                this.minutes = minutes;                this.hours = hours;                }                public double minutes(){                return minutes;                    }                    public double hours(){                return hours;                    }                public void addMinutes(){                if(this.minutes<59){                this.minutes++;                }else{                this.minutes=0;                this.hours++;                }                                }                public void addFiveMinutes(){                this.minutes += 5.0;                  }                public void addHours(){                this.hours = this.hours + hours;                }                public void addTwelveHours(){                this.hours += 12.0;                 }                  public static void main(String[] args) {//call default constructorClock myClock = new Clock();//call clock toString methodSystem.out.println("My Clock: \t\t\t"+myClock.toString()); //call constructor with two argumentsClock yourClock = new Clock(12,55);System.out.print("Your Clock: \t\t\t");yourClock.displayTime(); //call addMinutemyClock.addMinutes();System.out.print("My Clock (add 1 Minute): \t");//call showTime methodmyClock.displayTime(); //call addFiveMinutesmyClock.addFiveMinutes();System.out.print("My Clock (add 5 Minutes): \t");myClock.displayTime(); //call addHourmyClock.addHours();System.out.print("My Clock (add 1 Hour): \t\t");myClock.displayTime(); //call addTwelveHoursyourClock.addTwelveHours();System.out.print("Your Clock (twelve hours later):");yourClock.displayTime(); //call addTwelveHoursyourClock.addFiveMinutes();System.out.print("Your Clock (five minutes later):");yourClock.displayTime();    }    }
Link to comment
https://linustechtips.com/topic/308806-would-this-code-work/
Share on other sites

Link to post
Share on other sites

You need to work on your formatting - all those method signatures don't need indented, but the content inside it does, i.e:
 

public class Clock {	private double minutes;	private double hours;	public Clock() {		minutes = 0;		hours = 0;	}	public Clock(double minutes, double hours) {		this.minutes = minutes;		this.hours = hours;	}	public double minutes() {		return minutes;	}	public double hours() {		return hours;	}	public void addMinutes() {		if (this.minutes < 59) {			this.minutes++;		} else {			this.minutes = 0;			this.hours++;		}	}	public void addFiveMinutes() {		this.minutes += 5.0;	}	public void addHours() {		this.hours = this.hours + hours;	}	public void addTwelveHours() {		this.hours += 12.0;	}	public static void main(String[] args) {		// call default constructor		Clock myClock = new Clock();		// call clock toString method		System.out.println("My Clock: \t\t\t" + myClock.toString());		// call constructor with two arguments		Clock yourClock = new Clock(12, 55);		System.out.print("Your Clock: \t\t\t");		yourClock.displayTime();		// call addMinute		myClock.addMinutes();		System.out.print("My Clock (add 1 Minute): \t");		// call showTime method		myClock.displayTime();		// call addFiveMinutes		myClock.addFiveMinutes();		System.out.print("My Clock (add 5 Minutes): \t");		myClock.displayTime();		// call addHour		myClock.addHours();		System.out.print("My Clock (add 1 Hour): \t\t");		myClock.displayTime();		// call addTwelveHours		yourClock.addTwelveHours();		System.out.print("Your Clock (twelve hours later):");		yourClock.displayTime();		// call addTwelveHours		yourClock.addFiveMinutes();		System.out.print("Your Clock (five minutes later):");		yourClock.displayTime();	}}

Also, you've called a method "displayTime", but you've not created a method that does this. Why not do something like this instead:
 
       

 double minute = yourClock.minutes(); double hour = yourClock.hours(); System.out.println(String.format(hour + ":" + minute));

 
You can create a method displayTime using the guidelines above. Also I believe if you run your program, the line
 

System.out.println("My Clock: \t\t\t" + myClock.toString());

will create junk characters as you can't call the method toString on non-primitive types (someone correct me if I'm wrong).

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
https://linustechtips.com/topic/308806-would-this-code-work/#findComment-4197511
Share on other sites

Link to post
Share on other sites

You need to work on your formatting - all those method signatures don't need indented, but the content inside it does, i.e:

 

public class Clock {	private double minutes;	private double hours;	public Clock() {		minutes = 0;		hours = 0;	}	public Clock(double minutes, double hours) {		this.minutes = minutes;		this.hours = hours;	}	public double minutes() {		return minutes;	}	public double hours() {		return hours;	}	public void addMinutes() {		if (this.minutes < 59) {			this.minutes++;		} else {			this.minutes = 0;			this.hours++;		}	}	public void addFiveMinutes() {		this.minutes += 5.0;	}	public void addHours() {		this.hours = this.hours + hours;	}	public void addTwelveHours() {		this.hours += 12.0;	}	public static void main(String[] args) {		// call default constructor		Clock myClock = new Clock();		// call clock toString method		System.out.println("My Clock: \t\t\t" + myClock.toString());		// call constructor with two arguments		Clock yourClock = new Clock(12, 55);		System.out.print("Your Clock: \t\t\t");		yourClock.displayTime();		// call addMinute		myClock.addMinutes();		System.out.print("My Clock (add 1 Minute): \t");		// call showTime method		myClock.displayTime();		// call addFiveMinutes		myClock.addFiveMinutes();		System.out.print("My Clock (add 5 Minutes): \t");		myClock.displayTime();		// call addHour		myClock.addHours();		System.out.print("My Clock (add 1 Hour): \t\t");		myClock.displayTime();		// call addTwelveHours		yourClock.addTwelveHours();		System.out.print("Your Clock (twelve hours later):");		yourClock.displayTime();		// call addTwelveHours		yourClock.addFiveMinutes();		System.out.print("Your Clock (five minutes later):");		yourClock.displayTime();	}}

Also, you've called a method "displayTime", but you've not created a method that does this. Why not do something like this instead:

 

       

 double minute = yourClock.minutes(); double hour = yourClock.hours(); System.out.println(String.format(hour + ":" + minute));

 

You can create a method displayTime using the guidelines above. Also I believe if you run your program, the line

 

System.out.println("My Clock: \t\t\t" + myClock.toString());

will create junk characters as you can't call the method toString on non-primitive types (someone correct me if I'm wrong).

where would i put  System.out.println(String.format(hour + ":" + minute));?

Link to comment
https://linustechtips.com/topic/308806-would-this-code-work/#findComment-4197747
Share on other sites

Link to post
Share on other sites

You need to work on your formatting - all those method signatures don't need indented, but the content inside it does, i.e:

 

public class Clock {	private double minutes;	private double hours;	public Clock() {		minutes = 0;		hours = 0;	}	public Clock(double minutes, double hours) {		this.minutes = minutes;		this.hours = hours;	}	public double minutes() {		return minutes;	}	public double hours() {		return hours;	}	public void addMinutes() {		if (this.minutes < 59) {			this.minutes++;		} else {			this.minutes = 0;			this.hours++;		}	}	public void addFiveMinutes() {		this.minutes += 5.0;	}	public void addHours() {		this.hours = this.hours + hours;	}	public void addTwelveHours() {		this.hours += 12.0;	}	public static void main(String[] args) {		// call default constructor		Clock myClock = new Clock();		// call clock toString method		System.out.println("My Clock: \t\t\t" + myClock.toString());		// call constructor with two arguments		Clock yourClock = new Clock(12, 55);		System.out.print("Your Clock: \t\t\t");		yourClock.displayTime();		// call addMinute		myClock.addMinutes();		System.out.print("My Clock (add 1 Minute): \t");		// call showTime method		myClock.displayTime();		// call addFiveMinutes		myClock.addFiveMinutes();		System.out.print("My Clock (add 5 Minutes): \t");		myClock.displayTime();		// call addHour		myClock.addHours();		System.out.print("My Clock (add 1 Hour): \t\t");		myClock.displayTime();		// call addTwelveHours		yourClock.addTwelveHours();		System.out.print("Your Clock (twelve hours later):");		yourClock.displayTime();		// call addTwelveHours		yourClock.addFiveMinutes();		System.out.print("Your Clock (five minutes later):");		yourClock.displayTime();	}}
Also, you've called a method "displayTime", but you've not created a method that does this. Why not do something like this instead:

 

       

 double minute = yourClock.minutes(); double hour = yourClock.hours(); System.out.println(String.format(hour + ":" + minute));
 

You can create a method displayTime using the guidelines above. Also I believe if you run your program, the line

 

System.out.println("My Clock: \t\t\t" + myClock.toString());
will create junk characters as you can't call the method toString on non-primitive types (someone correct me if I'm wrong).
You are correct, it would give a bunch of stuff that would be of no use in this context. Unless you override the method in myClock to specifically return a string based on the clock
Link to comment
https://linustechtips.com/topic/308806-would-this-code-work/#findComment-4197799
Share on other sites

Link to post
Share on other sites

where would i put  System.out.println(String.format(hour + ":" + minute));?

Right underneath your other syso.out lines, i.e:

double minute = yourClock.minutes();double hour = yourClock.hours();System.out.println("Your Clock (five minutes later):");System.out.println(String.format(hour + ":" + minute));

But like I said, it would be alot cleaner if you create a method that does this for you, so all you'd need to do is something like:

System.out.println("Your Clock (five minutes later):");System.out.println(printTime(yourClock));

Let me know if you need any help with said method.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
https://linustechtips.com/topic/308806-would-this-code-work/#findComment-4203930
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×