Question

In: Computer Science

how to correct this java code so that i get the correct day of week? and...

how to correct this java code so that i get the correct day of week?

and test the year

public static void main(String[] args) {
       //
      
      
Scanner s = new Scanner(System.in);

//needed info
//year month, day
int year, month, dayOfMonth;

// add day of week , century yr
int dayOfWeek, century, yearOfCentury;
  
//user inputs year
System.out.print("Enter year: (example, 2020):");
year = s.nextInt();

//user inputs month by number
System.out.print("Enter month: 1-12:");
month = s.nextInt();


//user inputs day of month
System.out.print("Enter the day of the month: 1-31:");
dayOfMonth = s.nextInt();

// testing if month is January or February
if(month == 1) {
month = 13;
} else if(month == 2) {
month = 14;
}


//determining year month
century = year / 100;
yearOfCentury = year % 100;
dayOfWeek = (dayOfMonth + ((26 * (month + 1))/10) + yearOfCentury + yearOfCentury/4 + century/4 + century * 5) % 7;
  

//figuring out day
if(dayOfWeek == 0) {
System.out.print("Day of the week is Saturday.");
}
else if(dayOfWeek == 1) {
System.out.print("Day of the week is Sunday.");
}
else if(dayOfWeek == 2) {
System.out.print("Day of the week is Monday.");
}
else if(dayOfWeek == 3) {
System.out.print("Day of the week is Tuesday.");
}
else if(dayOfWeek == 4) {
System.out.print("Day of the week is Wednesday.");
}
else if(dayOfWeek == 5) {
System.out.print("Day of the week is Thursday.");
}
else if(dayOfWeek == 6) {
System.out.print("Day of the week is Friday.");
}


}
}

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

DayOfWeekDebug.java

package classes15;

import java.util.Scanner;

public class DayOfWeekDebug {

   public static void main(String[] args) {
       //
       Scanner s = new Scanner(System.in);
      
       //needed info
       //year month, day
       int year, month, dayOfMonth;

       // add day of week , century yr
       int dayOfWeek, century, yearOfCentury;

       //user inputs year
       System.out.print("Enter year: (example, 2020):");
       year = s.nextInt();
       if(year<0) {
           System.out.println("Error!!! Invalid year is entered.");
           return;
       }

       //user inputs month by number
       System.out.print("Enter month: 1-12:");
       month = s.nextInt();


       //user inputs day of month
       System.out.print("Enter the day of the month: 1-31:");
       dayOfMonth = s.nextInt();

       // testing if month is January or February
       if(month == 1) {
           month = 13;
       } else if(month == 2) {
           month = 14;
       }


       //determining year month
       century = year / 100;
       yearOfCentury = year % 100;
       dayOfWeek = (dayOfMonth + ((26 * (month + 1))/10) + yearOfCentury + yearOfCentury/4 + century/4 + century * 5) % 7;


       //figuring out day
       if(dayOfWeek == 0) {
           System.out.print("Day of the week is Saturday.");
       }
       else if(dayOfWeek == 1) {
           System.out.print("Day of the week is Sunday.");
       }
       else if(dayOfWeek == 2) {
           System.out.print("Day of the week is Monday.");
       }
       else if(dayOfWeek == 3) {
           System.out.print("Day of the week is Tuesday.");
       }
       else if(dayOfWeek == 4) {
           System.out.print("Day of the week is Wednesday.");
       }
       else if(dayOfWeek == 5) {
           System.out.print("Day of the week is Thursday.");
       }
       else if(dayOfWeek == 6) {
           System.out.print("Day of the week is Friday.");
       }


   }

}


Related Solutions

How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
Complete the code so that it can convert the date to day of week using python,...
Complete the code so that it can convert the date to day of week using python, the code should pass the doctest def convert_datetime_to_dayofweek(datetime_string): """ This function takes date in the format MON DAY YEAR HH:MM(PM/AM) and returns the day of the week Assume input string is UTC    >>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM') 'Wednesday' >>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM') 'Thursday' """ # code goes here
Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
Please can I kindly get a flowchart for this java code. Thank you. //import the required...
Please can I kindly get a flowchart for this java code. Thank you. //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in));...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT