Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part One: Chapter 3 Lab - Selection Control Structures Lab Objectives Be able to

ID: 3844185 • Letter: P

Question

Part One:

Chapter 3 Lab - Selection Control Structures

Lab Objectives

Be able to construct boolean expressions to evaluate a given condition

Be able to compare Strings

Be able to use a flag

Be able to construct if and if-else-if statements to perform a specific task

Be able to construct a switch statement

Be able to format numbers

Introduction

Up to this point, all the programs you have had a sequential control structure. This means that all statements are executed in order, one after another. Sometimes we need to let the computer make decisions, based on the data. A selection control structure allows the computer to select which statement to execute.

In order to have the computer make a decision, it needs to do a comparison. So we will work with writing boolean expressions. Boolean expressions use relational operators and logical operators to create a condition that can be evaluated as true or false.

Once we have a condition, we can conditionally execute statements. This means that there are statements in the program that may or may not be executed, depending on the condition. We can also chain conditional statements together to allow the computer to choose from several courses of action. We will explore this using nested if-else statements as well as a switch statement.

In this lab, we will be editing a pizza ordering program. It creates a Pizza object to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. The user will also receive a $2.00 discount if his/her name is Mike or Diane.

Task #1 The if Statement, Comparing Strings, and Flags

1. Copy the file PizzaOrder.java into NetBeans.

2. Compile and run PizzaOrder.java. You will be able to make selections, but at this point, you will always get a Hand-tossed pizza at a base cost of $12.99 no matter what you select, but you will be able to choose toppings, and they should add into the price correctly. You will also notice that the output does not look like money. So we need to edit PizzaOrder.java to complete the program so that it works correctly.

3. Construct a simple if statement. The condition will compare the String input by the user as his/her first name with the first names of the owners, Mike and Diane. Be sure that the comparison is not case sensitive.

4. If the user has either first name, set the discount flag to true. This will not affect the price at this point yet.

Task #2 The if-else-if Statement

1. Write an if-else-if statement that lets the computer choose which statements to execute by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.

2. The default else of the above if-else-if statement should print a statement that the user input was not one of the choices, so a 12 inch pizza will be made. It should also set the size to 12 and the cost to 12.99.

3. Compile, debug, and run. You should now be able to get correct output for size and price (it will still have Hand-tossed crust, the output won’t look like money, and no discount will be applied yet). Run your program multiple times ordering a 10, 12, 14, 16, and 17 inch pizza.

Task #3 Switch Statement

1. Write a switch statement that compares the user’s choice with the appropriate characters (make sure that both capital letters and small letters will work).

2. Each case will assign the appropriate string indicating crust type to the crust variable.

3. The default case will print a statement that the user input was not one of the choices, so a Hand-tossed crust will be made.

4. Compile, debug, and run. You should now be able to get crust types other than Hand-tossed. Run your program multiple times to make sure all cases of the switch statement operate correctly.

Task #4 Using a Flag as a Condition

1. Write an if statement that uses the flag as the condition. Remember that the flag is a Boolean variable, therefore is true or false. It does not have to be compared to anything.

2. The body of the if statement should contain two statements:

a) A statement that prints a message indicating that the user is eligible for a $2.00 discount.

b) A statement that reduces the variable cost by 2.

3. Compile, debug, and run. Test your program using the owners’ names (both capitalized and not) as well as a different name. The discount should be correctly at this time.

Task #5 Formatting Numbers

1. Add an import statement to use the DecimalFormat class as indicated above the class declaration.

2. Create a DecimalFormat object that always shows 2 decimal places.

3. Edit the appropriate lines in the main method so that any monetary output has 2 decimal places.

4. Compile, debug, and run. Your output should be completely correct at this time, and numeric output should look like money.

Pizza Order:

/**
This program allows the user to order a pizza
*/
import java.util.Scanner;
import java.text.DecimalFormat;

public class PizzaOrder
{
   public static void main (String [] args)
   {
       //TASK #5 Create a DecimalFormat object with 2 decimal places
       DecimalFormat money = new DecimalFormat ("0.00"); // Help - this is the DicimalFormat
      
       //Create a Scanner object to read input
       Scanner keyboard = new Scanner (System.in);
      
       String firstName;                   //user's first name
       boolean discount = false;       //flag, true if user is eligible for discount
       int inches;                           //size of the pizza
       char crustType;                   //code for type of crust
       String crust = "Hand-tossed"; //name of crust
       double cost = 12.99;               //cost of the pizza
       final double TAX_RATE = .08;   //sales tax rate
       double tax;                           //amount of tax
       char choice;                       //user's choice
       String input;                       //user input
       String toppings = "Cheese ";   //list of toppings  
       int numberOfToppings = 0;       //number of toppings
      
       //prompt user and get first name
       System.out.println("Welcome to Mike and Diane's Pizza");
       System.out.print("Enter your first name: ");
       firstName = keyboard.nextLine();
      
       //determine if user is eligible for discount by
       //having the same first name as one of the owners
       //ADD LINES HERE FOR TASK #1 step 3 use String function equalsIgnoreCase()
      
       //prompt user and get pizza size choice
       System.out.println("Pizza Size (inches) Cost");
       System.out.println(" 10 $10.99");
       System.out.println(" 12 $12.99");
       System.out.println(" 14 $14.99");
       System.out.println(" 16 $16.99");
       System.out.println("What size pizza would you like?");
       System.out.print("10, 12, 14, or 16 (enter the number only): ");
       inches = keyboard.nextInt();
      
       //set price and size of pizza ordered
       //ADD LINES HERE FOR TASK #2
               if (inches == 10)
       {
           cost = 10.99;
       }
       else if (inches == 12)
       {
           cost = 12.99;
       }
       // To do - Add inches 14, 16 here
       else
       {
           System.out.println("That was not one of the choices, " +
               "you will get a 12 inch pizza.");
           inches = 12;
           cost= 12.99;
       }

              
       //consume the remaining newline character
       keyboard.nextLine();  
      
       //prompt user and get crust choice
       System.out.println("What type of crust do you want? ");
       System.out.print("(H)Hand-tossed, (T) Thin-crust, or " +
           "(D) Deep-dish (enter H, T, or D): ");
       input = keyboard.nextLine();
       crustType = input.charAt(0);
              
       //set user's crust choice on pizza ordered
       //ADD LINES FOR TASK #3
       switch (crustType)
       {
           case 'H':
           case 'h':
               crust = "Hand-tossed";
               break;
           // To do - and for other crust
           default:
               System.out.println("That was not one of the choices, " +
                   "you will get Hand-tossed.");
               crust = "Hand-tossed";
       }

                              
       //prompt user and get topping choices one at a time                  
       System.out.println("All pizzas come with cheese.");
       System.out.println("Additional toppings are $1.25 each,"
               +" choose from");
       System.out.println("Pepperoni, Sausage, Onion, Mushroom");
  
       //if topping is desired,
       //add to topping list and number of toppings
       System.out.print("Do you want Pepperoni? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y')
       {
           numberOfToppings += 1;
           toppings = toppings + "Pepperoni ";
       }
       System.out.print("Do you want Sausage? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y')
       {
           numberOfToppings += 1;
           toppings = toppings + "Sausage ";
       }
       System.out.print("Do you want Onion? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y')
       {
           numberOfToppings += 1;
           toppings = toppings + "Onion ";
       }
       System.out.print("Do you want Mushroom? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y')
       {
           numberOfToppings += 1;
           toppings = toppings + "Mushroom ";
       }

       //add additional toppings cost to cost of pizza
       cost = cost + (1.25*numberOfToppings);
      
       //display order confirmation
       System.out.println();
       System.out.println("Your order is as follows: ");
       System.out.println(inches + " inch pizza");
       System.out.println(crust + " crust");
       System.out.println(toppings);      
      
       //apply discount if user is elibible
       //ADD LINES FOR TASK #4 HERE
      
       //EDIT PROGRAM FOR TASK #5
       //SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES
       System.out.println("The cost of your order is: $" + cost);
       // TASK #5 help - System.out.println("The cost of your order is: $" + money.format(cost));
          
       //calculate and display tax and total cost
       tax = cost * TAX_RATE;
       System.out.println("The tax is: $" + tax);
       System.out.println("The total due is: $" + (tax+cost));
      
       System.out.println("Your order will be ready for pickup in 30 minutes.");
   }  
}

---------------------------------------------------------------------------------------

Part Two:

Chapter 4 Lab
Loops and Files
Lab Objectives
Be able to write a while loop
Be able to write a do-while loop
Be able to write a for loop
Be able to use file streams for I/O
Be able to write a loop that reads until end of file
Be able to implement an accumulator and a counter
Introduction
This is a simulation of rolling dice. We will roll 10,000 times in our program. The theoretical
probability of rolling doubles of a specific number is 1 out of 36 or approximately 278 out of
10,000 times that you roll the pair of dice. Since this is a simulation, the numbers will vary a
little each time you run it. We will start with a while loop, then use the same program, changing
the while loop to a do-while loop, and then a for loop.
We will be introduced to file input and output. We will read a file, line by line, converting each
line into a number. We will then use the numbers to calculate the mean and standard deviation.
Task #1 Loops
1. Copy the file DiceSimulation.java into NetBeans or other Java IDE tools.
2. The double roll of 1s and 2s are given in the code. You can run the program to get a
result like:

Comment Prompt:

C: MyWorkhomework>java Dicesimulation

You rolled snake eyes 279 out of 10000 rolls.

You rolled double twos 275 out of 10000 rolls.

You rolled double threes 0 out of 10000 rolls.

You rolled double fours 0 out of 10000 rolls.

You rolled double sixes 0 out of 10000 rolls.

C: MyWorkhomework>

3. You follow the style to code for double roll value for 3 – 6 so all the doubles should be
around 278.
4. Change the while loop to do-while loop and run the program. Result should be similar.
5. Change the while loop to a for loop and run the program. Result should be similar.

Task #2 File Input and Output
1. Copy the files StatsDemo.java into NetBeans or other Java IDE tools.
2. Read “FilePath.doc” to know where to put the Numbers.txt.
3. The file input and output part is coded. Try to read through the code and understand the
program.
4. Try to run the program. Enter Numbers.txt when prompted. Check the Result.txt. There
should be just one line on the file to show “mean = 0.000”.
5. Modify the program to calculate the mean. Run the program. You should now get a mean
of 77.444 in the Result.txt

FilePath:

When dealing with files, file path is a key point.

1. Absolute file path – You can give input/output file an absolute file path so that the Java program knows where to find the file. The absolute file path in Windows starts from the dirve name , for example you can copy Numbers.txt to C: empmyhomework and input the file name as:

“C:\temp\myhomework\Numbers.txt”. Note: you need to use double “”.

DiceSimulation:

/**
This class simulates rolling a pair of dice 10,000 times and
counts the number of times doubles of are rolled for each different
pair of doubles.
*/

import java.util.Random;       //to use the random number generator
public class DiceSimulation
{
   public static void main(String[] args)
   {
       final int NUMBER = 10000;   //the number of times to roll the dice

       //a random number generator used in simulating rolling a dice
       Random generator = new Random();
      
       int die1Value;     // number of spots on the first die
       int die2Value;     // number of spots on the second die
       int count = 0;      // number of times the dice were rolled
       int snakeEyes = 0;     // number of times snake eyes is rolled
       int twos = 0;           // number of times double two is rolled
       int threes = 0;       // number of times double three is rolled
       int fours = 0;           // number of times double four is rolled
       int fives = 0;           // number of times double five is rolled
       int sixes = 0;           // number of times double six is rolled

       //ENTER YOUR CODE FOR THE ALGORITHM HERE
       while(count < NUMBER)
       {
           die1Value = generator.nextInt(6) + 1;   //returns 1,2,3,4,5,or 6
           die2Value = generator.nextInt(6) + 1;   //returns 1,2,3,4,5,or 6

           if(die1Value == die2Value)
           {
               if (die1Value == 1)
                   snakeEyes++;
               else if (die1Value == 2)
                   twos++;
               // Task #1 step 3: To do - code for die1Value = 3, 4, 5, and 6
           }
           count++;
       }

       System.out.println ("You rolled snake eyes " + snakeEyes +
           " out of " + count + " rolls.");
       System.out.println ("You rolled double twos " + twos +
           " out of " + count + " rolls.");
       System.out.println ("You rolled double threes " + threes +
           " out of " + count + " rolls.");
       System.out.println ("You rolled double fours " + fours +
           " out of " + count + " rolls.");
       System.out.println ("You rolled double fives " + fives +
           " out of " + count + " rolls.");
       System.out.println ("You rolled double sixes " + sixes +
           " out of " + count + " rolls.");
   }
}

StatsDemo:

import java.text.DecimalFormat;   //for number formatting
import java.util.Scanner;       //for keyboard input
import java.io.*; // for file input and output

public class StatsDemo
{
   public static void main(String [] args) throws IOException //ADD A THROWS CLAUSE HERE - throws IOException
   {
       double sum = 0;       //the sum of the numbers
       int count = 0;       //the number of numbers added
       double mean = 0;     //the average of the numbers
       double stdDev = 0;   //the standard deviation of the numbers
       String line;       //a line from the file
       double difference;   //difference between the value and the mean

       //create an object of type Decimal Format
       DecimalFormat threeDecimals = new DecimalFormat("0.000");
       //create an object of type Scanner
       Scanner keyboard = new Scanner (System.in);
       String filename;   // the user input file name

       //Prompt the user and read in the file name
       System.out.println("This program calculates statistics"
           + "on a file containing a series of numbers");
       System.out.print("Enter the file name: "); // user should enter Numbers.txt
       filename = keyboard.nextLine();

       // Create a file reader
       FileReader freader = new FileReader(filename);
       //Create a BufferedReader object passing it the FileReader object.
       BufferedReader input = new BufferedReader(freader);
      
       // read the first line of the file
       line = input.readLine();
       //loop that continues until you are at the end of the file
       while (line != null)
       {
           //convert the line into a double value and add the value to the sum
           sum += Double.parseDouble(line);
           //increment the counter
           count++;
           //read a new line from the file
           line = input.readLine();
       }
      
       //close the input file
       input.close();
      
       // Task #2 step 5: To do - calculate mean
       // mean = sum/count;
      
       //create an object of type FileWriter using “Results.txt”
       FileWriter fwriter = new FileWriter("Results.txt");
       //create an object of PrintWriter passing it the FileWriter object.
       PrintWriter output = new PrintWriter(fwriter);

       //print the results to the output file
       output.println("mean = " + threeDecimals.format(mean));
      
       //close the output file
       output.close();
   }
}

--------------------------------------------------------------------------

Part Three:

Chapter 5 Lab - Methods

Lab Objectives

Be able to write methods

Be able to call methods

Introduction

Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can be called as many times as needed without rewriting the code each time.

Task #1 void Methods

1. Copy the file Geometry.java to your NetBeans source directory. This program will compile and run.

2. Above the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will simply print out instructions for the user with a menu of options for the user to choose from. The menu should appear to the user as:

This is a geometry calculator

Choose what you would like to calculate

1. Find the area of a circle

2. Find the area of a rectangle

3. Find the area of a triangle

4. Find the circumference of a circle

5. Find the perimeter of a rectangle

6. Find the perimeter of a triangle

Enter the number of your choice:

3. Add a line in the main method that calls the printMenu method as indicated by the comments.

4. Compile, debug, and run. You should be able to choose any option, but you will always get 0 for the answer except the first one that has example for you. We will fix this in the next task.

Task #2 Value-Returning Methods

1. Write a static method called circleArea that takes in the radius of the circle and returns the area using the formula A = X r X r .

2. Write a static method called rectangleArea that takes in the length and width of the rectangle and returns the area using the formula A = lw.

3. Write a static method called triangleArea that takes in the base and height of the triangle and returns the area using the formula A = ½bh.

4. Write a static method called circleCircumference that takes in the radius of the circle and returns the circumference using the formula C = 2r.

5. Write a static method called rectanglePerimeter that takes in the length and the width of the rectangle and returns the perimeter of the rectangle using the formula P = 2l +2w.

6. Write a static method called trianglePerimeter that takes in the lengths of the three sides of the triangle and returns the perimeter of the triangle which is calculated by adding up the three sides.

Task #3 Calling Methods

Add lines in the main method in the Geometry class which will call these methods. The comments indicate where to place the method calls. Compile, debug, and run. Test out the program using your sample data.

Geometry:

import java.util.Scanner;

/**
   This program demonstrates static methods
*/

public class Geometry
{
  
   /**
       The printMenu method prints the menu for the user to choose from
   */
   public static void printMenu ()
   {
       System.out.println("This is a geometry calculator");
       System.out.println("Choose what you would like to calculate");
       System.out.println("1. Find the area of a circle");
       // To do - Add more menu items mentioned on Task #1
   }
  
   /**
       The circleArea method returns the area of the circle
       @param radius The radius of the circle
       @return The area of the circle
   */
   public static double circleArea(double radius)
   {
       return Math.PI * radius * radius;
   }
  
   // To do - Add more method follow the style of circleArea for Task #2
  
   public static void main (String [] args)
   {
       int choice;           //the user's choice
       double value = 0.0;       //the value returned from the method
       char letter;       //the Y or N from the user's decision to exit
       double radius;       //the radius of the circle
       double length;       //the length of the rectangle
       double width;       //the width of the rectangle
       double height;       //the height of the triangle
       double base;       //the base of the triangle
       double side1;       //the first side of the triangle
       double side2;       //the second side of the triangle
       double side3;       //the third side of the triangle
      
       //create a scanner object to read from the keyboard
       Scanner keyboard = new Scanner (System.in);
      
       //do loop was chose to allow the menu to be displayed first
       do
       {
           printMenu();
           choice = keyboard.nextInt();
                      
           switch (choice)
           {
               case 1:
                   System.out.print("Enter the radius of the circle: ");
                   radius = keyboard.nextDouble();
                   value = circleArea(radius);
                   System.out.println("The area of the circle is " + value);
                   break;
               case 2:
                   System.out.print("Enter the length of the rectangle: ");
                   length = keyboard.nextDouble();
                   System.out.print("Enter the width of the rectangle: ");
                   width = keyboard.nextDouble();
                   //To do for Task #3 - call the rectangleArea method and store the result in the value variable
                   // follow the style in case 1          
                   System.out.println("The area of the rectangle is " + value);
                   break;
               case 3:
                   System.out.print("Enter the height of the triangle: ");
                   height = keyboard.nextDouble();
                   System.out.print("Enter the base of the triangle: ");
                   base = keyboard.nextDouble();
                   //To do for Task #3 - call the triangleArea method and store the result in the value variable
                   // follow the style in case 1  
                   System.out.println("The area of the triangle is " + value);
                   break;
               case 4:
                   System.out.print("Enter the radius of the circle: ");
                   radius = keyboard.nextDouble();
                   //To do for Task #3 - call the circumference method and store the result in the value variable
                   // follow the style in case 1  
                   System.out.println("The circumference of the circle is " + value);
                   break;
               case 5:
                   System.out.print("Enter the length of the rectangle: ");
                   length = keyboard.nextDouble();
                   System.out.print("Enter the width of the rectangle: ");
                   width = keyboard.nextDouble();
                   //To do for Task #3 - call the perimeter method and store the result in the value variable
                   // follow the style in case 1  
                   System.out.println("The perimeter of the rectangle is " + value);
                   break;
               case 6:
                   System.out.print("Enter the length of side 1 of the triangle: ");
                   side1 = keyboard.nextDouble();
                   System.out.print("Enter the length of side 2 of the triangle: ");
                   side2 = keyboard.nextDouble();
                   System.out.print("Enter the length of side 3 of the triangle: ");
                   side3 = keyboard.nextDouble();
                   //To do for Task #3 - call the perimeter method and store the result in the value variable
                   // follow the style in case 1  
                   System.out.println("The perimeter of the triangle is " + value);
                   break;
               default:
                   System.out.println("You did not enter a valid choice.");
           }  
           keyboard.nextLine(); //consumes the new line character after the number                      
           System.out.println("Do you want to exit the program (Y/N)?: ");
           String answer = keyboard.nextLine();
           letter = answer.charAt(0);
       }while (letter != 'Y' && letter != 'y');
   }

} // End of the class

----------------------------------------------------------------------------

Part Four:

Chapter 6 Lab - Classes and Objects

Lab Objectives

Be able to declare a new class

Be able to write a constructor

Be able to write instance methods that return a value

Be able to write instance methods that take arguments

Be able to instantiate an object

Be able to use calls to instance methods to access and change the state of an object

Introduction

We will write a television class. A television object will have the following attributes (fields in the class):

manufacturer. The manufacturer attribute will hold the brand name, such as Sony, Toshiba, etc. This cannot change once the television is created, so will be a named constant.

screenSize. The screenSize attribute will hold the size of the television screen. This cannot change once the television has been created so will be a named constant.

powerOn. The powerOn attribute will hold the value true if the power is on, and false if the power is off.

channel. The channel attribute will hold the value of the station that the television is showing.

volume. The volume attribute will hold a number value representing the loudness (0 being no sound).

The television object will also be able to control the state of its attributes. These controls become methods in our class.

setChannel. The setChannel method will store the desired station in the channel field.

power. The power method will toggle the power between on and off, changing the value stored in the powerOn field from true to false or from false to true.

increaseVolume. The increaseVolume method will increase the value stored in the volume field by 1.

decreaseVolume. The decreaseVolume method will decrease the value stored in the volume field by 1.

getChannel. The getChannel method will return the value stored in the channel field.

getVolume. The getVolume method will return the value stored in the volume field.

getManufacturer. The getManufacturer method will return the constant value stored in the MANUFACTURER field.

getScreenSize. The getScreenSize method will return the constant value stored in the SCREEN_SIZE field.

We will also need a constructor method that will be used to create an instance of a Television.

Task #1 Creating a Television Class

See the given Television.java file.

The 2 constant fields and the 3 remaining fields listed in the UML diagram have been declared.

Read and try to understand the constructor that has two parameters, a manufacturer’s brand and a screen size. These parameters will bring in information.

Initialize the powerOn field to false (power is off), the volume to 20, and the channel to 2 inside the constructor.

Compile and fix the syntax errors. Do not run.

Task #2 Methods

Define access methods called getChannel, getManufacturer, and getScreenSize that return the value of the corresponding field. Follow the sample given as getVolume.

Read and try to understand the method given in the Televesion.java. Define decreaseVolume method that will decrease the volume by 1.

Compile and fix the syntax errors. Do not run.

Task #3 Run the application

You can only execute (run) a program that has a main method, so there is a driver program that is already written to test out your Television class. Copy the file TelevisionDemo.java to the same directory as Television.java.

Compile and run TelevisionDemo and follow the prompts. If your output matches the output below, Television.java is complete and correct. You will not need to modify it further for this lab.

OUTPUT (boldface is user input)

A 55 inch Toshiba has been turned on.

What channel do you want? 56

Channel: 56 Volume: 21

Too loud!! I am lowering the volume.

Channel: 56 Volume: 15

Task #4 Create another instance of a Television

Edit the TelevisionDemo.java file. Declare another Television object called portable.

Instantiate portable to be a Sharp 19 inch television.

Use a call to the power method to turn the power on.

Use calls to the accessor methods to print what television was turned on.

Use calls to the mutator methods to change the channel to the user’s preference and decrease the volume by two.

Use calls to the accessor methods to print the changed state of the portable.

Compile and run TelevisionDemo again. See the given output sample (outputSample.doc)

Television:

/**The purpose of this class is to madel a television*/
public class Television
{
   private String MANUFACTURER;   //the maker of the television
   private int SCREEN_SIZE;       //the size of the television
   private boolean powerOn;       //contains true if the TV power is on
   private int channel;               //the numeric station setting
   private int volume;               //the numeric level of the sound
  
   /**Constructor creates a television with given brand and size
   @param brand The manufacturer of the television
   @param size The size of the screen
   */
   public Television(String brand, int size)
   {
       MANUFACTURER = brand;
       SCREEN_SIZE = size;
       // To do - Task 1 step 4
   }
  
   /**accessor method returns the current volume
   @return the current volume
   */
   public int getVolume()
   {
       return volume;
   }
  
   /**accessor method returns the current channel
   @return the current channel
   */
   // To do - Add code here
  
   /**accessor method returns the manufacturer's name
   @return the television manufacture's name
   */
   // To do - Add code here
  
   /**accessor method returns the size of the screen
   @return the size of the screen
   */
   // To do - Add code here
  
   /**mutator method stores the desired station in the channel field
   @param station the desired television channel
   */
   public void setChannel(int station)
   {
       channel = station;
   }
  
   /**toggles the power on and off*/
   public void power()
   {
       powerOn = !powerOn;
   }
  
   /**increases the volume by one*/
   public void increaseVolume()
   {
       volume++;
   }
  
   /**decreases the volume by one*/
// To do - Add code here
}
  

TelevisionDemo:

/** This class demonstrates the Television class*/

import java.util.Scanner;

public class TelevisionDemo
{
   public static void main(String[] args)
   {
       //create a Scanner object to read from the keyboard
       Scanner keyboard = new Scanner (System.in);

       //declare variables
       int station;   //the user's channel choice

       //declare and instantiate a television object
       Television bigScreen = new Television("Toshiba", 55);
       //turn the power on
       bigScreen.power();
       //display the state of the television
       System.out.println("A " + bigScreen.getScreenSize() + " inch " +
           bigScreen.getManufacturer()   + " has been turned on.");
       //prompt the user for input and store into station
       System.out.print("What channel do you want? ");
       station = keyboard.nextInt();

       //change the channel on the television
       bigScreen.setChannel(station);
       //increase the volume of the television
       bigScreen.increaseVolume();
       //display the the current channel and volume of the television
       System.out.println("Channel: " + bigScreen.getChannel() +
           " Volume: "   + bigScreen.getVolume());
       System.out.println("Too loud!! I am lowering the volume.");
       //decrease the volume of the television
       bigScreen.decreaseVolume();
       bigScreen.decreaseVolume();
       bigScreen.decreaseVolume();
       bigScreen.decreaseVolume();
       bigScreen.decreaseVolume();
       bigScreen.decreaseVolume();
       //display the current channel and volume of the television
       System.out.println("Channel: " + bigScreen.getChannel() +
           " Volume: "   + bigScreen.getVolume());
       System.out.println();    //for a blank line


       //HERE IS WHERE YOU DO TASK #4
       //Television portable = new Television("Sharp", 19);
       //turn the power on
      
       //display the state of the television
      
       //prompt the user for input and store into station
      
       //change the channel on the television
      
       //decrease the volume of the television
      
       //display the current channel and volume of the television
      
   }
}

Output Sample:

---------------------------------------------ThankYou----------------------

SQ004 720V05 (C:) temp JavaApplication2 organize Open Views Burn Name Favorite Links build Documents nbproject Pictures Src Music applet policy More build.xml manifest.mf Folders Numbers txt Results,txt Application? Java build Classes avaapplication2 nbproject Src JavaScript Oracle spring workFD workspace TOSAPINS Search Date modified Type 7/28/2012 2:25 PM File Folder 4/21/2012 7:33 AM File Folder 9/20/2012 6:19 PM File Folder 4/21/2012 7:33 AM POLICY File 4/10/2012 8:15 PM ML Document 4/10/2012 8:15 PM MIF File 8/22/2005 6:10 AM Text Document 9/20/2012 6:25 PM Text Document Size 1 K 4 KB 1 K 20 KB 1 KB

Explanation / Answer

Here is yoour code for part 1.

package fileOpsAssignment;

import java.util.Scanner;
import java.text.DecimalFormat;

public class PizzaOrder {
   public static void main(String[] args) {
       // TASK #5 Create a DecimalFormat object with 2 decimal places
       DecimalFormat money = new DecimalFormat("0.00"); // Help - this is the
                                                           // DicimalFormat

       // Create a Scanner object to read input
       Scanner keyboard = new Scanner(System.in);

       String firstName; // user's first name
       boolean discount = false; // flag, true if user is eligible for discount
       int inches; // size of the pizza
       char crustType; // code for type of crust
       String crust = "Hand-tossed"; // name of crust
       double cost = 12.99; // cost of the pizza
       final double TAX_RATE = .08; // sales tax rate
       double tax; // amount of tax
       char choice; // user's choice
       String input; // user input
       String toppings = "Cheese "; // list of toppings
       int numberOfToppings = 0; // number of toppings

       // prompt user and get first name
       System.out.println("Welcome to Mike and Diane's Pizza");
       System.out.print("Enter your first name: ");
       firstName = keyboard.nextLine();

       // determine if user is eligible for discount by
       // having the same first name as one of the owners
       // ADD LINES HERE FOR TASK #1 step 3 use String function
       // equalsIgnoreCase()
       if(firstName.equalsIgnoreCase("Mike") || firstName.equalsIgnoreCase("Diane")) {
           discount = true;
       }

       // prompt user and get pizza size choice
       System.out.println("Pizza Size (inches) Cost");
       System.out.println(" 10 $10.99");
       System.out.println(" 12 $12.99");
       System.out.println(" 14 $14.99");
       System.out.println(" 16 $16.99");
       System.out.println("What size pizza would you like?");
       System.out.print("10, 12, 14, or 16 (enter the number only): ");
       inches = keyboard.nextInt();

       // set price and size of pizza ordered
       // ADD LINES HERE FOR TASK #2
       if (inches == 10) {
           cost = 10.99;
       } else if (inches == 12) {
           cost = 12.99;
       } else if (inches == 14) {
           cost = 14.99;
       } else if (inches == 16) {
           cost = 16.99;
       }
       else {
           System.out.println("That was not one of the choices, " + "you will get a 12 inch pizza.");
           inches = 12;
           cost = 12.99;
       }

       // consume the remaining newline character
       keyboard.nextLine();

       // prompt user and get crust choice
       System.out.println("What type of crust do you want? ");
       System.out.print("(H)Hand-tossed, (T) Thin-crust, or " + "(D) Deep-dish (enter H, T, or D): ");
       input = keyboard.nextLine();
       crustType = input.charAt(0);

       // set user's crust choice on pizza ordered
       // ADD LINES FOR TASK #3
       switch (crustType) {
       case 'H':
       case 'h':
           crust = "Hand-tossed";
           break;
       case 'T':
       case 't':
           crust = "Thin-crust";
           break;
       case 'D':
       case 'd':
           crust = "Deep-dish";
           break;
       // To do - and for other crust
       default:
           System.out.println("That was not one of the choices, " + "you will get Hand-tossed.");
           crust = "Hand-tossed";
       }

       // prompt user and get topping choices one at a time
       System.out.println("All pizzas come with cheese.");
       System.out.println("Additional toppings are $1.25 each," + " choose from");
       System.out.println("Pepperoni, Sausage, Onion, Mushroom");

       // if topping is desired,
       // add to topping list and number of toppings
       System.out.print("Do you want Pepperoni? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y') {
           numberOfToppings += 1;
           toppings = toppings + "Pepperoni ";
       }
       System.out.print("Do you want Sausage? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y') {
           numberOfToppings += 1;
           toppings = toppings + "Sausage ";
       }
       System.out.print("Do you want Onion? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y') {
           numberOfToppings += 1;
           toppings = toppings + "Onion ";
       }
       System.out.print("Do you want Mushroom? (Y/N): ");
       input = keyboard.nextLine();
       choice = input.charAt(0);
       if (choice == 'Y' || choice == 'y') {
           numberOfToppings += 1;
           toppings = toppings + "Mushroom ";
       }
       // add additional toppings cost to cost of pizza
       cost = cost + (1.25 * numberOfToppings);

       // display order confirmation
       System.out.println();
       System.out.println("Your order is as follows: ");
       System.out.println(inches + " inch pizza");
       System.out.println(crust + " crust");
       System.out.println(toppings);

       // apply discount if user is elibible
       // ADD LINES FOR TASK #4 HERE
       if(!discount) {
           System.out.println("You are eligible for a $2.00 discount.");
           cost = cost - 2;
       }
       // EDIT PROGRAM FOR TASK #5
       // SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES
       System.out.println("The cost of your order is: $" + cost);
       // TASK #5 help -
       System.out.println("The cost of your order is: $" +
               money.format(cost));

       // calculate and display tax and total cost
       tax = cost * TAX_RATE;
       System.out.println("The tax is: $" + money.format(tax));
       System.out.println("The total due is: $" + money.format(tax + cost));

       System.out.println("Your order will be ready for pickup in 30 minutes.");
   }
}

Sample Run: -

Welcome to Mike and Diane's Pizza
Enter your first name: Mike
Pizza Size (inches) Cost
10 $10.99
12 $12.99
14 $14.99
16 $16.99
What size pizza would you like?
10, 12, 14, or 16 (enter the number only): 14
What type of crust do you want?
(H)Hand-tossed, (T) Thin-crust, or (D) Deep-dish (enter H, T, or D): T
All pizzas come with cheese.
Additional toppings are $1.25 each, choose from
Pepperoni, Sausage, Onion, Mushroom
Do you want Pepperoni? (Y/N): Y
Do you want Sausage? (Y/N): Y
Do you want Onion? (Y/N): N
Do you want Mushroom? (Y/N): N

Your order is as follows:
14 inch pizza
Thin-crust crust
Cheese Pepperoni Sausage
The cost of your order is: $17.490000000000002
The cost of your order is: $17.49
The tax is: $1.40
The total due is: $18.89
Your order will be ready for pickup in 30 minutes.