Sorry again guys having another logical error and i\'m sure its something really
ID: 3637196 • Letter: S
Question
Sorry again guys having another logical error and i'm sure its something really easy that i'm just not seeing so hopefully some outside perspective will help. Once again this is custom code. The code will not return the Total Seconds after it receives input from the user.
//Creates a public time class to store static conversion methods for a TimeDemo Main Class
public class Time {
//static method to convert hours to minutes
public static Integer hours2minutes(int hour){
int minutes = 0;
minutes = (hour/60);
return minutes;
}
//static method to calculate minutes to seconds
public static Integer minutes2seconds(int minutes){
int seconds = 0;
seconds = (minutes/60);
return seconds;
}
//static method to calculate seconds to seconds
public static Integer seconds2seconds(int seconds2){
int userSeconds = 0;
seconds2 = userSeconds;
return seconds2;
}
//static method to calculate seconds to hours
public static Integer seconds2hours(int seconds2){
int hours = 0;
if (seconds2 >= 3600)
hours = (seconds2 / 60);
else
System.out.println("Calculates less than one hour.");
return hours;
}
//Static method to calculate total seconds
public static Integer convert(int seconds, int minutes, int hours){
int totalSeconds;
totalSeconds = seconds + minutes + (hours * 360);
return totalSeconds;
}
}
//TimeDemo Program
import javax.swing.JOptionPane;
public class TimeDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Time t1 = new Time();
int input = 0;
int hours = 0;
int minutes = 0;
int seconds= 0;
JOptionPane.showInputDialog("Enter number of hours: ");
hours = t1.hours2minutes(input);
JOptionPane.showInputDialog("Enter number of minutes: ");
minutes = t1.minutes2seconds(input);
JOptionPane.showInputDialog("Enter number of seconds: ");
seconds = t1.seconds2seconds(input);
JOptionPane.showMessageDialog(null, "Total number of seconds: " + Time.convert(seconds, minutes, hours));
}
}
Explanation / Answer
I've made some changes to your code. It's now working fine :) //Creates a public time class to store static conversion methods for a TimeDemo Main Class public class Time { //static method to convert hours to minutes public static Integer hours2minutes(int hour){ int minutes = 0; minutes = (hour*60); return minutes; } //static method to calculate minutes to seconds public static Integer minutes2seconds(int minutes){ int seconds = 0; seconds = (minutes*60); return seconds; } //static method to calculate seconds to seconds public static Integer seconds2seconds(int seconds2){ return seconds2; } //static method to calculate seconds to hours public static Integer seconds2hours(int seconds2){ int hours = 0; if (seconds2 >= 3600) hours = (seconds2 / 60); else System.out.println("Calculates less than one hour."); return hours; } //Static method to calculate total seconds public static Integer convert(int seconds, int minutes, int hours){ int totalSeconds; totalSeconds = seconds + minutes + (hours * 60); return totalSeconds; } } //TimeDemo Program import javax.swing.JOptionPane; public class TimeDemo { public static void main(String[] args) { // TODO Auto-generated method stub Time t1 = new Time(); int input = 0; int hours = 0; int minutes = 0; int seconds= 0; String temp; temp = JOptionPane.showInputDialog("Enter number of hours: "); input = Integer.parseInt(temp); hours = t1.hours2minutes(input); temp = JOptionPane.showInputDialog("Enter number of minutes: "); input = Integer.parseInt(temp); minutes = t1.minutes2seconds(input); temp = JOptionPane.showInputDialog("Enter number of seconds: "); input = Integer.parseInt(temp); seconds = t1.seconds2seconds(input); JOptionPane.showMessageDialog(null, "Total number of seconds: " + Time.convert(seconds, minutes, hours)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.