Question

In: Computer Science

This is in java, thank you! Problem2: In this problem you ask the user for a...

This is in java, thank you!

Problem2: In this problem you ask the user for a month and day. You will then calculate the season based on the month and day.

  • Create a class named Problem2
    • Create two integer instance variables, month and day.
    • Create a constructor to initialize the both variables. The constructor should accept two arguments as parameters, m and d. Initialize the instance variables with these two values.
    • Create a method called calcSeason. It should not have any arguments and should return a string.
    • The method should implement the following algorithm:
      • If month is 1,2, or 3, season = “Winter”
      • Else if month is 4,5, or 6, season = “Spring”
      • Else if month is 7,8, or 9, season = “Summer”
      • Else season = “Fall”
      • If month is evenly divisible by 3 and day >=21, then
        • If season is “Winter” then season = “Spring”
        • Else if season is “Spring”, season = “Summer”
        • Else if season is “Summer” season = “Fall”
        • Else season = “Winter
      • Return Season
    • Create a class named Problem2Tester (reminder: all tester classes have a main method)
      • Import Scanner
      • Create a Scanner object
      • Prompt the use to enter a month as a number
      • Get the number from the user and store it as a variable
      • Prompt the use to enter a day as a number
      • Get the number from the user and store it as a variable
      • Create a Problem2 object, initializing with the month and day from the user
      • Call the calcSeason method and print the result this can be done directly in the print statement or you can create a new variable and print it)

Solutions

Expert Solution

Below is the source code for the two classes:

1. Problem2 class:

/*
* class for calculating the season
*/
public class Problem2 {
   //month and day variables
   private int month;
   private int day;
  
   //constructor
   public Problem2(int m, int d) {
       month = m;
       day = d;
   }
   /*
   * Method to calculate the season
   */
   public String calcSeason() {
       String season = "";
      
       if ((month == 1) || (month == 2) || (month == 3)) {
           season = "Winter";
       }
       else if ((month == 4) || (month == 5) || (month == 6)) {
           season = "Spring";
       }
       else if ((month == 7) || (month == 8) || (month == 9)) {
           season = "Summer";
       }
       else {
           season = "Fall";
       }
       //if month is divisible by 3 and day is greater than or equal to 21
       if ((month%3 == 0) && (day >= 21)) {
           if (season == "Winter") {
               season = "Spring";
           }
           else if (season == "Spring") {
               season = "Summer";
           }
           else if (season == "Summer") {
               season = "Fall";
           }
           else {
               season = "Winter";
           }
       }
       return season;
   }
}

2. Problem2Tester Class:

import java.util.Scanner;
/**
* Class for testing the Problem2
*/
public class Problem2Tester {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Print the month as number: ");
       int month = sc.nextInt();
       System.out.println("Print the day as number: ");
       int day = sc.nextInt();
       Problem2 pr = new Problem2(month, day);
       System.out.println("Season is: " + pr.calcSeason());
   }
}

Please comment for any further queries. Thanks


Related Solutions

Write a java code to ask the user for a string, and assuming the user enters...
Write a java code to ask the user for a string, and assuming the user enters a string containing character "a", your java program must convert all "a" to "b" in the string so for example if user types : "AMAZON" , your program must convert it to "BMBZON" the driver program must ask : enter a string containing "a" or "A" --------- (user enters string) then it should say; HERE IS YOUR NEW STRING ---------- (string with all a's...
Java guessing game: For this program you will use Linear Search. - Ask the user to...
Java guessing game: For this program you will use Linear Search. - Ask the user to choose between a number from 0 to 2000. - The user will guess how long the linear search will take in nanoseconds, - After the user puts in their answer, show the user the difference between the actual answer and their answer. (Example: The user puts in 5 nanoseconds. The actual time is 11 nanoseconds. The program will show 6 nanoseconds.) There should be...
Java Ask the user to input a letter grade in either upper or lower case. You...
Java Ask the user to input a letter grade in either upper or lower case. You are to output a message depending on the grade. When the input is A or a, output the word "Excellent!" When the input is B or b, output "Very Good" When the input is C or c, output "Satisfactory" For any other input you must output the word "Fail".
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Code in Java Change the instructionLabel to ask the user to enter a numeric value into...
Code in Java Change the instructionLabel to ask the user to enter a numeric value into the textField and click the button to convert the entered value from kilometers to miles (1.609344 km = 1 mile). When the actionButton on the form is clicked, ActionWindow should take the value entered in the textField, convert it from kilometers to miles, and display the result in the resultField. import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class...
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation.
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Here is the code that I have so far I can't seem to get it...
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation. This is the code I have from the...
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT