Beyond Verses

My blog that specialize in Space Science and Latest news from NASA

Monday, October 27, 2008

Assignment 3 prog 3 (Timing)

note : I add Input function to this program

import javax.swing.JOptionPane;
public class Time {
private int hours;
private int minutes;
private int seconds;

//default constructor
public Time()
{
hours = 0;
minutes = 0;
seconds = 0;
}

//parameter constructor
public Time(int a,int b,int c)
{
hours = a;
minutes = b;
seconds = c;
}

public int getHours()
{
return hours;
}

public int getMinutes()
{
return minutes;
}

public int getSeconds()
{
return seconds;
}

public void addmin(int a)
{
minutes = minutes + a;
do
if (minutes>=60){
this.hours++;
minutes = minutes - 60;
}
while (minutes >=60);
}

public void addsec(int a)
{
seconds = seconds + a;
do
if (seconds>=60){
this.minutes++;
seconds = seconds - 60;
}
while (seconds>=60);
}

// Display
public void displayTime()
{
System.out.println("Hours="+hours);
System.out.println("Minutes="+minutes);
System.out.println("Seconds="+seconds);
}

public String tostring()
{
String x;
x = "Hours="+hours+" Minutes="+minutes+" Seconds="+seconds;
return x;
}


// main area
public static void main(String[]args)
{
// Input
String v1 = JOptionPane.showInputDialog("enter Houres ");
String v2 = JOptionPane.showInputDialog("enter Minutes");
String v3 = JOptionPane.showInputDialog("enter Seconds");
String v4 = JOptionPane.showInputDialog("enter add Minutes");
String v5 = JOptionPane.showInputDialog("enter add Seconds");

int d1 = Integer.parseInt(v1);
int d2 = Integer.parseInt(v2);
int d3 = Integer.parseInt(v3);
int d4 = Integer.parseInt(v4);
int d5 = Integer.parseInt(v5);

//call constructor
Time s1 = new Time(d1,d2,d3);

//Add
s1.addmin(d4);
s1.addsec(d5);
s1.displayTime();

//Display
String m;
m = s1.tostring();
System.out.println(m);
}

}

2 comments:

Ahmed Zizo said...

well done ya Ahmed basha,,
but u needn't use the if statement in the do while..use while instead "Do not do redundant checks",,,or make the if statement before the do while ,,,
and u should handle the increment of the hours in the add min mehtod ,,and use the addmin(1) method instead of using min++;

BUT Finally ,well done,,,

Ahmed Abd El-hameed Hamdy said...

thank you ahmed here I am at the began Do the best I can