Question

Can someone walk through this problem with as much explanation as possible. Thank you import java.io.*;...

Can someone walk through this problem with as much explanation as possible.

Thank you

import java.io.*;
import java.util.Scanner;

class Lesson_32_Activity_One
{
    public static void monthName(int m)
    {

        //Use if statements to check for each month individually.
        //Alternatively, students may create a String array and use
        //the variable m to access the month, as in the following commented out code.
        /*
         * String [] months = {"January", "February", "March", "April", "May",
     *                      "June", "July", "August", "September", "October",
     *                      "November", "December"};
     * return months[m - 1];
     */
         if (m == 1)
            System.out.println("January");
        if (m == 2)
            System.out.println("February");
        if (m == 3)
            System.out.println("March");
        if (m ==4)
            System.out.println("April");
        if (m == 5)
            System.out.println("May");
        if (m == 6)
            System.out.println("June");
        if (m == 7)
            System.out.println("July");
        if (m == 8)
            System.out.println("August");
        if (m == 9)
            System.out.println("September");
        if (m == 10)
            System.out.println("October");
        if (m == 11)
            System.out.println("November");
        if (m == 12)
            System.out.println("December");
    }

    //main method to test the program. This is not required by the code-runner,
    //but is an important step in writing new methods.
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter a month number:");

        int m = scan.nextInt();

        System.out.println("monthName(" + m + ") prints:");
        monthName(m);
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

/**
* This program reads a month number from the user
*
* and prints the respective month name in output 1 for January 2 February etc..
*
*/
public class Lesson_32_Activity_One
{
public static void monthName(int m)
{

//Use if statements to check for each month individually.
//Alternatively, students may create a String array and use
//the variable m to access the month, as in the following commented out code.
/*
* String [] months = {"January", "February", "March", "April", "May",
* "June", "July", "August", "September", "October",
* "November", "December"};
* return months[m - 1];
*/
   //using if condition to check like input is 1 than print January if 2 print February etc....
if (m == 1)
System.out.println("January");
if (m == 2)
System.out.println("February");
if (m == 3)
System.out.println("March");
if (m ==4)
System.out.println("April");
if (m == 5)
System.out.println("May");
if (m == 6)
System.out.println("June");
if (m == 7)
System.out.println("July");
if (m == 8)
System.out.println("August");
if (m == 9)
System.out.println("September");
if (m == 10)
System.out.println("October");
if (m == 11)
System.out.println("November");
if (m == 12)
System.out.println("December");
}

//main method to test the program. This is not required by the code-runner,
//but is an important step in writing new methods.
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);

//reading month number from user
System.out.println("Enter a month number:");

int m = scan.nextInt();
//printing the month name for the given number
System.out.println("monthName(" + m + ") prints:");
monthName(m);
}
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Can someone walk through this problem with as much explanation as possible. Thank you import java.io.*;...
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
  • 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)){...

  • How to add exceptions to this code for each month so that it won't exceed the...

    How to add exceptions to this code for each month so that it won't exceed the amount of days that in each one for them. Example there are only 28 days in feb and 31 days in march. import java.sql.Timestamp; public class RandomDate { public static void main(String[] args) { long begin=Timestamp.valueOf("1900-01-01 00:00:00").getTime(); long end=Timestamp.valueOf("2014-12-31 00:00:00").getTime(); long diff=end-begin+1; Timestamp rand=new Timestamp(begin+(long)(Math.random()*diff));    int month=rand.getMonth()+1,day=rand.getDate(),year=rand.getYear()+1900;    String[] months={"Janunary","February","March","April","May","June","July","August","September","October","November","December"}; String suffix="th"; if(day==2)suffix="nd"; else if(day==3)suffix="rd"; else if(day==1)suffix="st";    System.out.println("Random Date is: "+month+"/"+day+"/"+year); System.out.println("Formatted...

  • debug the program in java /** This program demonstrates an array of String objects. It should...

    debug the program in java /** This program demonstrates an array of String objects. It should print out the months and each day of the month. It should print out the month name and days for the month with the least days It should print out the average number of days in each month Here is what it should print when executed: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has...

  • 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) {...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random;...

    Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • If anyone can please convert from Java to python. Thank you!! import javax.swing.*; import javax.swing.event.*; import...

    If anyone can please convert from Java to python. Thank you!! import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class CalenderProgram{ static JLabel lblMonth, lblYear; static JButton btnPrev, btnNext; static JTable tblCalendar; static JComboBox cmbYear; static JFrame frmMain; static Container pane; static DefaultTableModel mtblCalendar; //Table model static JScrollPane stblCalendar; //The scrollpane static JPanel pnlCalendar; static int realYear, realMonth, realDay, currentYear, currentMonth;    public static void main (String args[]){ //Look and feel try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

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