Question

In: Computer Science

Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry {...

Language java

Rewrite the method getMonthusing the "this" parameter

CODE:

import java.util.Scanner;

public class DateSecondTry

{

private String month;

private int day;

private int year; //a four digit number.

public void writeOutput()

{

System.out.println(month + " " + day + ", " + year);

}

public void readInput()

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter month, day, and year.");

System.out.println("Do not use a comma.");

month = keyboard.next();

day = keyboard.nextInt();

year = keyboard.nextInt();

}

public int getDay()

{

return day;

}

public int getYear()

{

return year;

}

public int getMonth()

{

if (month.equalsIgnoreCase("January")) return 1;

else if (month.equalsIgnoreCase("February")) return 2;

else if (month.equalsIgnoreCase("March")) return 3;

else if (month.equalsIgnoreCase("April")) return 4;

else if (month.equalsIgnoreCase("May")) return 5;

else if (month.equalsIgnoreCase("June")) return 6;

else if (month.equalsIgnoreCase("July")) return 7;

else if (month.equalsIgnoreCase("August")) return 8;

else if (month.equalsIgnoreCase("September")) return 9;

else if (month.equalsIgnoreCase("October")) return 10;

else if (month.equalsIgnoreCase("November"))  return 11;

else if (month.equalsIgnoreCase("December")) return 12;

else

{

System.out.println("Fatal Error");

System.exit(0);

return 0; //Needed to keep the compiler happy

}

}

}

Solutions

Expert Solution

import java.util.Scanner;

public class DateSecondTry {
    private String month;
    private int day;
    private int year; //a four digit number.

    public void writeOutput() {
        System.out.println(month + " " + day + ", " + year);
    }

    public void readInput() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter month, day, and year.");
        System.out.println("Do not use a comma.");
        month = keyboard.next();
        day = keyboard.nextInt();
        year = keyboard.nextInt();
    }

    public int getDay() {
        return day;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        if (this.month.equalsIgnoreCase("January")) return 1;
        else if (this.month.equalsIgnoreCase("February")) return 2;
        else if (this.month.equalsIgnoreCase("March")) return 3;
        else if (this.month.equalsIgnoreCase("April")) return 4;
        else if (this.month.equalsIgnoreCase("May")) return 5;
        else if (this.month.equalsIgnoreCase("June")) return 6;
        else if (this.month.equalsIgnoreCase("July")) return 7;
        else if (this.month.equalsIgnoreCase("August")) return 8;
        else if (this.month.equalsIgnoreCase("September")) return 9;
        else if (this.month.equalsIgnoreCase("October")) return 10;
        else if (this.month.equalsIgnoreCase("November")) return 11;
        else if (this.month.equalsIgnoreCase("December")) return 12;
        else {
            System.out.println("Fatal Error");
            System.exit(0);
            return 0; //Needed to keep the compiler happy
        }
    }
}

Related Solutions

I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[]...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[] args) { char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char userAnswers[] = new char[correctAnswers.length]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < userAnswers.length; i++) { String answer = ""; System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1); do...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // As a hint, you should not have to use the name of each // month more than once. // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month...
import java.util.Scanner; public class Months { // You will need to write a method that makes...
import java.util.Scanner; public class Months { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month = input.nextInt(); String output = monthAsString(month); System.out.println(output); } }
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT