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 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.
Ask the user for a filename, then ask the user for a line of text. Write...
Ask the user for a filename, then ask the user for a line of text. Write the line of text the filename specified by the user. The filename may be absolute or relative to the project directory.
Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code. I cannot read handwriting....
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
.......Subject Java..... main() main() will ask the user for input and then call functions to do...
.......Subject Java..... main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main(). Second function Create another function named computeBill that receives 2 parameters. It will receive the...
In java, ask the user for a filename. Display the oldest car for every manufacturer from...
In java, ask the user for a filename. Display the oldest car for every manufacturer from that file. Files Given: car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt (they are too big for question) Required Output: Standard Input                 Files in the same directory car-list-1.txt car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt Enter filename\n Oldest cars by make\n Acura Legend 1989 2T1BPRHE8EC858192\n Audi 80/90 1988 2GKALMEK4E6424961\n Bentley Continental Flying Spur 2006 WBA3G7C5XFK430850\n BMW 600 1959 1N6AA0ED2FN639969\n Bugatti Veyron 2009 SCFEBBBC5AG474636\n Buick Century 1987 2HNYD18625H455550\n Cadillac DeVille 1994...
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT