Question

I was presented with this problem: Create a Java class MonthNames with a single static method...

I was presented with this problem:

Create a Java class MonthNames with a single static method getMonthName which Takes a single integer corresponding to a month of the year, 1 representing January through 12 representing December, and Returns a string for the name of the month.

This is what I have so far

public class MonthNames {

   public static String getMonthName (int numMonth) {

       return "month"; }

       public static void main (String [] args) {

           System.out.println ("begin tests");

          

           System.out.println ("Enter Month 1");

           System.out.println ("Expect: January");

           System.out.println ("Actual: " + getMonthName (1));

          

           System.out.println ("Enter Month 6");

           System.out.println ("Expect: June");

           System.out.println ("Actual: " + getMonthName (6));

          

           System.out.println ("Enter Month 12");

           System.out.println ("Expect: December");

           System.out.println ("Actual: " + getMonthName (12));

          

           System.out.println ("End tests");

          

           if(numMonth = 1) { System.out.println("January"); }

           else(numMonth = 2) { System.out.println("February"); }

           else(numMonth = 3) { System.out.println("March"); }

           else(numMonth = 4) { System.out.println("April"); }

           else(numMonth = 5) { System.out.println("May"); }

           else(numMonth = 6) { System.out.println("June"); }

           else(numMonth = 7) { System.out.println("July"); }

           else(numMonth = 8) { System.out.println("August"); }

           else(numMonth = 9) { System.out.println("September"); }

           else(numMonth = 10) { System.out.println("October"); }

           else(numMonth = 11) { System.out.println("November"); }

           else(numMonth = 12) { System.out.println("December"); }

           else {System.out.println("Not a month"); }

What else do i need to do to this code in order to make it work? (JAVA)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class MonthNames {
    public static String getMonthName(int numMonth) {
        if (numMonth == 1) {
            return "January";
        } else if (numMonth == 2) {
            return "February";
        } else if (numMonth == 3) {
            return "March";
        } else if (numMonth == 4) {
            return "April";
        } else if (numMonth == 5) {
            return "May";
        } else if (numMonth == 6) {
            return "June";
        } else if (numMonth == 7) {
            return "July";
        } else if (numMonth == 8) {
            return "August";
        } else if (numMonth == 9) {
            return "September";
        } else if (numMonth == 10) {
            return "October";
        } else if (numMonth == 11) {
            return "November";
        } else if (numMonth == 12) {
            return "December";
        } else {
            return "Not a month";
        }
    }

    public static void main(String[] args) {
        System.out.println ("begin tests");

        System.out.println ("Enter Month 1");
        System.out.println ("Expect: January");
        System.out.println ("Actual: " + getMonthName (1));

        System.out.println ("Enter Month 6");
        System.out.println ("Expect: June");
        System.out.println ("Actual: " + getMonthName (6));

        System.out.println ("Enter Month 12");
        System.out.println ("Expect: December");
        System.out.println ("Actual: " + getMonthName (12));

        System.out.println ("End tests");

    }
}
Add a comment
Know the answer?
Add Answer to:
I was presented with this problem: Create a Java class MonthNames with a single static method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Please review this Java program a sim getitng 3 errors. Public class StarPattern { public static...

    Please review this Java program a sim getitng 3 errors. Public class StarPattern { public static void main(String[] args) {   finalint rows=7;intspaces=rows/2;intstars=1;for(inti=0;i<rowas;i++)   {    for(inti=0;j<spaces;j++)    {     System.out.println('*');}    for(inti=0;j<stars;j++)    {     System.out.println('*");}                      System.out.println();            if (i<rows/2){spaces--;stars+=2;}             else{spaces++;stars-=2;              }            }            }            }

  • help me with a question in java: class A { int i = 10; } class...

    help me with a question in java: class A { int i = 10; } class B extends A { int i = 20; } public class Boo { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }

  • import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); }    if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • JAVA QUESTION - BalancedParentheses Implement the class BalancedParentheses with a method isBalanced that receives a String...

    JAVA QUESTION - BalancedParentheses Implement the class BalancedParentheses with a method isBalanced that receives a String and checks whether the brackets in it are balanced. import java.util.Scanner; public class BalancedParentheses { public static void main(String[] args) { checkParentheses("((a + b) * t/2 * (1 - t)"); checkParentheses("(a + b) * t)/(2 * (1 - t)"); checkParentheses("a + ((a + b) * t)/(2 * (1 - t))"); System.out.println("Enter an expression: "); Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); checkParentheses(s);...

  • Is there any way that I can call method in other class without constructor in java?...

    Is there any way that I can call method in other class without constructor in java? public class Chest { private String contents; public String getContents()    {        return contents;    } public void setContents(String contents)    {               this.contents = contents;           } I need to set content and print out in the main method, but it keep gives me a error message when I try Chest.setContents("Gold"); on main class. I suppose...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT