Complete using the Java Language Syntax: Im struggling with how to create the to
ID: 667295 • Letter: C
Question
Complete using the Java Language Syntax:
Im struggling with how to create the toString method in the concert class that can be used in the main method using the variables "startTime", and "endTime" int the concert class.. please complete a working program following the instructions. i have also included the the Time class source code..
thanks
// This is the code from the Time class
public class Time
{
private int hours;
private int minutes;
private int seconds;
public Time(int h, int m, int s)
{
setTime(h, m, s);
}
public Time()
{
}
public void setHours(int h)
{
hours = ((h >= 0 && h < 24) ? h : 0);
}
public void setMinutes(int m)
{
minutes = ((m >= 0 && m < 60) ? m : 0);
}
public void setSeconds(int s)
{
seconds = ((s >= 0 && s < 60) ? s : 0);
}
public void setTime(int h, int m, int s)
{
setHours(h);
setMinutes(m);
setSeconds(s);
}
public String toMilitary()
{
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
@Override
public String toString()
{
return String.format("%d:%02d:%02d %s",
(hours == 0 || hours == 12) ? 12 : hours % 12, minutes,
seconds, (hours < 12) ? "AM" : "PM");
}
}
public class Time
{
private int hours;
private int minutes;
private int seconds;
public Time(int h, int m, int s)
{
setTime(h, m, s);
}
public Time()
{
}
public void setHours(int h)
{
hours = ((h >= 0 && h < 24) ? h : 0);
}
public void setMinutes(int m)
{
minutes = ((m >= 0 && m < 60) ? m : 0);
}
public void setSeconds(int s)
{
seconds = ((s >= 0 && s < 60) ? s : 0);
}
public void setTime(int h, int m, int s)
{
setHours(h);
setMinutes(m);
setSeconds(s);
}
public String toMilitary()
{
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
@Override
public String toString()
{
return String.format("%d:%02d:%02d %s",
(hours == 0 || hours == 12) ? 12 : hours % 12, minutes,
seconds, (hours < 12) ? "AM" : "PM");
}
}
Explanation / Answer
Program code:
//Time class: Time.java
public class Time
{
//declaration of variables
private int hours;
private int minutes;
private int seconds;
//Parameterized constructor
public Time(int h, int m, int s)
{
//call setTime function
setTime(h, m, s);
}
//default constructor
public Time()
{
}
//setHours method that sets the hours
public void setHours(int h)
{
hours = ((h >= 0 && h < 24) ? h : 0);
}
//setMinutes method that sets the minutes
public void setMinutes(int m)
{
minutes = ((m >= 0 && m < 60) ? m : 0);
}
//setSeconds method that sets the seconds
public void setSeconds(int s)
{
seconds = ((s >= 0 && s < 60) ? s : 0);
}
//setTime method that sets the Time
public void setTime(int h, int m, int s)
{
//call set methods of hours, minutes and seconds
setHours(h);
setMinutes(m);
setSeconds(s);
}
//toMilitary method that sets the format of the Time and
//returns a String value
public String toMilitary()
{
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
//toString method that sets the time format
public String toString()
{
return String.format("%d:%02d:%02d %s",
(hours == 0 || hours == 12) ? 12 : hours % 12, minutes,
seconds, (hours < 12) ? "AM" : "PM");
}
}
-----------------------------------------------------------------------------------------
//Concert.java class
public class Concert
{
//declare the variables name
//Time objects startTime, and endTime
String name;
Time startTime;
Time endTime;
//constructor
Concert(String n, Time s, Time e)
{
//Initialize the variables
name=n;
startTime=s;
endTime=e;
}
//toString method that returns the string value
public String toString()
{
String s="";
s+="The concert ""+name+"" starts at "+
startTime.toString()+" and ends at "+
endTime.toString()+". ";
return s;
}
}
----------------------------------------------------------------------------------------------
//import the scanner class from the util package
import java.util.*;
//ConcertApps.java
public class ConcertApps
{
//main method
public static void main(String args[])
{
//scanner object
Scanner scan=new Scanner(System.in);
//declare the variables
String name;
int hours, minutes, seconds;
Time start;
Time end;
//prompt the user to enter the name of the Concert
System.out.println("Enter the name of the Concert: ");
name=scan.nextLine();
//prompt the user to enter the starting time of
//the concert
System.out.println("Enter starting time in hour's minute's seconds: ");
hours=scan.nextInt();
minutes=scan.nextInt();
seconds=scan.nextInt();
//create a time object
start=new Time(hours, minutes, seconds);
//prompt the user to enter the ending time of the
//concert
System.out.println("Enter ending time in hour's minute's seconds: ");
hours=scan.nextInt();
minutes=scan.nextInt();
seconds=scan.nextInt();
//create another time object
end=new Time(hours, minutes, seconds);
//Create object of the Concert class
Concert con=new Concert(name, start, end);
//print the final result
System.out.println(con.toString());
}
}
---------------------------------------------------------------------------------------------------------------------------
Sample output:
Enter the name of the Concert:
It's My Life Music
Enter starting time in hour's minute's seconds:
11 45 00
Enter ending time in hour's minute's seconds:
20 30 00
The concert "It's My Life Music" starts at 11:45:00 AM and ends at 8:30:00 PM.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.