How to Display Days in Order for My Clock: Java Homework Solution

In summary: I want to see your steps.Step 1: Convert 2:30pm to minutes. 2 * 60 = 120, 120 + 30 = 150 minutes.Step 2: Add 585 minutes to 150 minutes. 150 + 585 = 735 minutes total.Step 3: Convert back to hours and minutes. 735 / 60 = 12 hours. 735 % 60 = 15 minutes.Step 4: Determine the day of the week. 735 % 1440 (minutes in a day) = 735 minutes.Step 5: Since the current day is Thursday and the total minutes is 735, the clock should now be at 12:15
  • #1
Albert12
9
0
How to display days in order for my clock :

Code:
Thursday, 12:00 pm
Thursday, 2:30 pm
Friday, 12:15 am
Friday, 12:15 am
Thursday, 2:30 pm
Saturday, 2:30 pm

I don't know how to do that. Con some pody help me out?

I have problem in this code ,I couldn't figure out method to display days in order:
Code:
public void advance(int m){
        int totalminutes = (hours*60)+(minutes+m);
              
        hours = (totalminutes/60)%24;
        minutes=totalminutes%60;
         day_of_week=totalminutes%9;
    }

    public void reverse(int m){

	int totalminutes = (hours*60)-(minutes-m);
       
        hours = (totalminutes%24)/60;
        minutes=totalminutes%60;
         day_of_week=totalminutes%14;

    }
Here entyer the cod for my homework :

Code:
package clockdriver;

/**
 *
 * @author
 */
public class ClockDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Clock MyClock = new Clock("Thursday", 12,0); // Instantiate a clock object

	System.out.println(MyClock); // Should display: Thursday, 12:00 p.m.
        System.out.println(); // print blank line

        MyClock.advance(150); // advance time 150 minutes (2 hours 30 minutes)
	System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.
        System.out.println();

        MyClock.advance(585); // advance time by 585 minutes (9 hours 45 minutes)
	System.out.println(MyClock); // Should display: Friday, 12:15 a.m.
        System.out.println();

        MyClock.advance(10080); // advance time by 10080 minutes (7 days)
	System.out.println(MyClock); // Should display: Friday, 12:15 a.m.
        System.out.println();
        MyClock.reverse(585); // reverse time by 585 minutes (9 hours 45 minutes)
	System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.
        System.out.println();

        MyClock.reverse(17280); // reverse time by 17280 minutes (12 days)
	System.out.println(MyClock); // Should display: Saturday, 2:30 p.m.
        System.out.println();

    } // end of main

} // end of class ClockDriver
class Clock{
    /***************************************************************
     * Objects of this class represent time (in hours and minutes) *
     * The time data is stored in 24 hour (HHMM military format)   *
     ***************************************************************/
    private int day_of_week; // valid range of values is 1 thru' 7
    private int hours; // range of values is 0 thru' 23
    private int minutes; // range of values is 0 thru' 59
 private static final String[] Days={"Saturday","Sunday","Monday","Tusday","Wensday","Thusday","Friday",};

    public Clock(String S, int h, int m){

	day_of_week = Integer.parseInt("6");
	
        hours=0;
        minutes=0;
    }



    public void advance(int m){
        int totalminutes = (hours*60)+(minutes+m);
              
        hours = (totalminutes/60)%24;
        minutes=totalminutes%60;
         day_of_week=totalminutes%9;
    }

    public void reverse(int m){

	int totalminutes = (hours*60)-(minutes-m);
       
        hours = (totalminutes%24)/60;
        minutes=totalminutes%60;
         day_of_week=totalminutes%14;

    }

    @Override
    public String toString(){

        String s= Days[day_of_week - 1]+","+
                (hours>12? hours-12 : hours<1? 12 : hours) +
                ":" + (minutes<10? ("0"+minutes) : minutes) +
                (hours>=12? " am" : " pm");

/*+ (hours>12? hours-12 : hours<1? 12 : hours) + ":" +
        (minutes<10? ("0"+minutes) : minutes) + (hours>=12? " pm" : " am"));*/
        
       return(s);










         
    }
    }

my currently result is

Code:
run:
Thusday,12:00 pm

Thusday,2:30 pm

Thusday,12:15 am

Thusday,12:15 am

Sunday,12:30 pm

Sunday,12:30 pm

BUILD SUCCESSFUL (total time: 0 seconds)
 
Physics news on Phys.org
  • #2
I don't understand what you're doing in the last lines of these two methods:
Code:
public void advance(int m){
        int totalminutes = (hours*60)+(minutes+m);
              
        hours = (totalminutes/60)%24;
        minutes=totalminutes%60;
        day_of_week=totalminutes%9;
    }

    public void reverse(int m){

	int totalminutes = (hours*60)-(minutes-m);
       
        hours = (totalminutes%24)/60;
        minutes=totalminutes%60;
        day_of_week=totalminutes%14;
    }
In the first method, why are you calculating totalminutes & 9? In the second method, why are you calculating totalminutes & 14?

There are 1,440 minutes in a day (24 * 60), but your clock starts at 12:00pm (noon), so if totalminutes > 720 but less than 2160, you need to increment the day to Friday. If totalminutes is between 2160 and 3600, you need to increment the day to Saturday. And so on.
 
  • #3
Can you please write miss code that , I need. I really stuk with it and I have no time. due time tonight. I tryed by this way but it doesn't work.
Code:
 public void advance(int m){
        int totalminutes = (hours*60)+(minutes+m);
              
        hours = (totalminutes/60)%24;
        minutes=totalminutes%60;

               day_of_week=(24*60)*7+10080/8;

    }

    public void reverse(int m){

	int totalminutes = (hours*60)-(minutes-m);
       
        hours = (totalminutes%24)/60;
        minutes=totalminutes%60;
         day_of_week=(24*60)*7+10080;
 
  • #4
This isn't an improvement. The first value that is coming up wrong in your program is when the clock is advanced 585 minutes. The program should display Friday 1:45 am, not Friday 12:15 am as your code comment shows. Work through by hand what you need to calculate to arrive at that day and time. Then write your code to do the same.
 
  • #5
Is this the way how to start I keep change minutes from 60 to 120 to 180 and time changed but I didnt reach the right one:

Code:
int totalminutes = (hours*558)+(minutes+m);
             
        hours = (totalminutes/60)%24;
        minutes=totalminutes%60;
 
  • #6
Now you're just guessing. First off, it was 585 minutes, not 558 minutes. Why do you have hours * 558?
 
  • #7
can you please give me example, I really don't understand. can you write code for me as example. I worry about time.
 
  • #8
I did houre*558 to change time and then it will change days. This is what I thought it will work!
 
Last edited:
  • #9
Code:
 public void advance(int m){
        int totalminutes = (hours*585)+(minutes+m);
             
        hours = (totalminutes/60)%24;
        minutes=totalminutes%60;

               day_of_week=totalminutes%8;

    }

    public void reverse(int m){

	int totalminutes = (hours*17280)-(minutes-m);
        
      
        hours = (totalminutes%24)/60;
        minutes=totalminutes%60;
         day_of_week=totalminutes%8;

    }
result:

Code:
run:
Thusday,12:00 am

Thusday,2:30 am

Saturday,5:45 am

Sunday,1:30 am

Monday,12:15 am

Saturday,12:45 am

BUILD SUCCESSFUL (total time: 0 seconds)
 
  • #10
If the current time and day is 2:30pm Thursday, and you advanced the clock by 585 minutes, show me how you would figure out the correct time and day.

I DON'T want to see code.
 

1. What is Java and why is it used for homework problems?

Java is a high-level, object-oriented programming language that is commonly used in academic settings. It is used for homework problems because it is versatile, easy to learn, and has a large community of support.

2. How do I approach solving a homework problem in Java?

The first step is to carefully read and understand the problem. Then, break it down into smaller tasks and come up with a plan for how to tackle each one. It is helpful to create a flowchart or pseudocode before writing any actual code.

3. What are some common mistakes students make when working on Java homework problems?

Some common mistakes include not properly understanding the problem, not following syntax rules, and not testing their code thoroughly. It is also important to pay attention to details, such as using the correct data types and variable names.

4. How can I improve my Java skills for homework problems?

Practice is key when it comes to improving Java skills. It is also helpful to read through and analyze code written by others, as well as seeking help from online resources or a tutor if needed.

5. Is it okay to collaborate with classmates on Java homework problems?

It is generally acceptable to discuss and brainstorm solutions with classmates, but it is important to write and submit your own original code. Copying or plagiarizing code is not only unethical, but it also prevents you from truly understanding and learning the material.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
58
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
5K
Back
Top