Question

The ability to read through Java™ code and predict the results, given specific inputs, is an...

The ability to read through Java™ code and predict the results, given specific inputs, is an extremely useful skill.

For this assignment, you will be analyzing the Java™ code in the linked zip file, and predicting the results given specific input.

Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document:

  1. What is the output of the program as it is written?
  2. What would the output of the program be if you assigned iAmHappy to false, num to 35, and numDayOfWeek to 5?
  3. What would the output of the program be if you assigned the iAmHappy to 5, num to 50, and numDayOfWeek to 8?

**********************************************************************
* Program Summary: This program demonstrates these basic Java concepts:
* - defining variables of different types
* - if-then and if-then-else logic
* - switch logic
***********************************************************************/

package prg420_week2_supportingactivity;


public class PRG420Week2_AnalyzeAssignment {

public static void main(String args[]) {
  
// Example of if-then logic.
// If a condition is true, do something. If it is not true, do nothing.

boolean iAmHappy = true; // We can set a boolean variable to true or false (no quotes around either)
if (iAmHappy != false) { // We could also have said "iAmHappy == true" here and gotten the same result.
System.out.println("if-then example: I am happy today!");
}

// Example of if-then-else logic.
// If a condition is true, do something. If it is not true, do something else.

int num = 120; // The num variable is set to 120
if( num < 50 ) { // Since 120 is NOT less than 50
       // the compiler will skip the next line and go directly to "else"           
System.out.println("if-then-else example: num " + num + " is less than 50"); }
else { // Since 120 is greater than 50, the next code line will print.
System.out.println("if-then-else example: " + num + " is greater than or equal to 50");
}

// Example of switch logic.
// If a condition involving one of several pre-defined values is met, do something.
// If the value contains none of the pre-defined values, do something else (typically display an error).

int numDayOfWeek = 3; // Declare numDayOfWeek and assign it 3, meaning the third day of the week
String dayString; // Declare a variable of type String to hold the name of the day
switch (numDayOfWeek) { // Examine the value of numDayOfWeek variable
case 1: dayString = "Monday"; // If numDayOfWeek is 1, then it must be Monday
break;
case 2: dayString = "Tuesday"; // If numDayOfWeek is 2, then it must be Thursday
break;
case 3: dayString = "Wednesday"; // etc.
break;
case 4: dayString = "Thursday";
break;
case 5: dayString = "Friday";
break;
case 6: dayString = "Saturday";
break;
case 7: dayString = "Sunday"; // There are only 7 days in a week, but we will add the default case
break; // to handle unexpected data. In this example, we are setting numDayOfWeek in our code, but
// in a real program it would come from somewhere else--and might be < 1 or > 7 for all we know.
default: dayString = "Error! There are exactly 7 days in a week";
break;
}
System.out.println("switch example: Starting with Monday as day 1, the day of the week for " + numDayOfWeek + " is " + dayString + ".");
}


}

Homework Answers

Answer #1

Answer:

a.

The output of the program is as follows:

if-then example: I am happy today !

if-then-else example: 120 is greater than or equal to 50

switch example: Starting with Monday as day 1, the day of the week for 3 is Wednesday.

Explanation:

1st part prints "I am happy today !" if the boolean variable iAmHappy is true

2nd part checks if 120 is greater than 50 or not then it prints that it is greater than 50 otherwise it would print the number is less than 50 if the number was less than 50

3rd part uses a switch statement to print the 3rd day of week as wednesday. The program passes numDayOfWeek as argument as switch and assigns a string of day according to the number.

b.

if variable iAmHappy is set to false ,then the program will not print anything and move on to next output.

if num is set to 35,then it will print "num 35 is less than 50"

if numDayOfWeek is set to 5 then it will print "Starting with Monday as day 1, the day of the week for 5 is Friday. "

The output is as follows:

if-then-else example: num 35 is less than 50
switch example: Starting with Monday as day 1, the day of the week for 5 is Friday.

c.

if we assign iAmHappy to 5,then the program will throw an error stating that you can't assign an integer variable to a boolean

if we set num to 50, it will print "50 is greater than or equal to 50"

if we set numDayOfWeek to 8, it will pick the default case of the switch statement to print i.e., "Error! There are exactly 7 days in a week."

Know the answer?
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
Similar Questions
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
Here is the code I am supposed to Analyze: // If we want to use the...
Here is the code I am supposed to Analyze: // If we want to use the Scanner class (type) to get user input, we need // to import the following Java package that includes the Scanner class // definitions. We do not have to add an import statement to use the // print() or println() methods of the System object, because they // are built in. import java.util.Scanner; public class PRG420Week1_AnalyzeAssignment { /* The main() method you see below is...
Download the attached .java file. Run it, become familiar with its processes. Your task is to...
Download the attached .java file. Run it, become familiar with its processes. Your task is to turn TemperatureConversion into GUI based program. it should, at the least, perform similar functions as their text output versions. The key factor to remember is that the workings should remain the same (some tweaks may be necessary) between text and GUI programs, while the means pf visual presentation and user interaction changes. You must properly document, comment, indent, space, and structure both programs. import...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
2.) Analyze the modified Java code PrimeFinderInterupt Make this program to RUN and provide a screenshot...
2.) Analyze the modified Java code PrimeFinderInterupt Make this program to RUN and provide a screenshot of the output Describe the additional functionality of the PrimeFinderInterrupt NOTE IF your original PrimeFinder ran faster than 7 seconds, change the code line: "if (++index > 7 )" to have a number less than the time to run your original code. _______________________________________________________________ Below is the "PrimeFinderInterupt" java code: package com.java24hours; public class PrimeFinderInterrupt implements Runnable { Thread go; StringBuffer primes = new StringBuffer();...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else {...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...