Question

In: Computer Science

For the second part of the Lab, improve the following Java program with functions. You should...

  1. For the second part of the Lab, improve the following Java program with functions. You should look for places in the code where similar statements are repeatedand think about how towrite function(s) to replace those statements. Unlike the first part of the Lab, in this exercise you will probably want to create one or more functions that return a value, and use the returned values in the rest of the program.

This is the Painting.javasource file for you to modify (bluelines are my additions):

public class Painting

{

   public static void main(String[] args)

   {

        double width, length, wallArea, ceilingArea;

        final double HEIGHT = 8;

      

        System.out.println("Calculation of Paint Requirements");

        

        System.out.print("Enter room length: ");

        length = Keyboard.nextDouble();

          // these 2 lines will make a nice function, promptDouble,

          // then replace those 2 lines with this single line:

          // length = promptDouble("Enter room length: ");

        System.out.print("Enter room width: ");

        width = Keyboard.nextDouble();

          // these 2 lines should be replaced in the same way:

          // width = promptDouble("Enter room width: ");

        

        wallArea = 2 * (length + width) * HEIGHT; // ignore doors

          // this line should use a perimeterfunction instead, as in:

          // wallArea = perimeter(length, width) * HEIGHT;

        ceilingArea = length * width;

          // this line should use a new function called area,

          // similar to perimeter, like so:

          // ceilingArea = area(length, width);

        System.out.println("The wall area is " + wallArea +

             " square feet.");

        System.out.println("The ceiling area is " + ceilingArea +

             " square feet.");

   }

}

Your job in this Lab is to write these three functions and make the above changesin the Java program Painting.java.

Solutions

Expert Solution

import java.util.Scanner;

public class Painting {

    public static Scanner Keyboard = new Scanner(System.in);

    public static double promptDouble(String s) {
        System.out.print(s);
        return Keyboard.nextDouble();
    }

    public static double perimeter(double length, double width) {
        return 2 * (length + width);
    }

    public static double area(double length, double width) {
        return length * width;
    }

    public static void main(String[] args) {
        double width, length, wallArea, ceilingArea;
        final double HEIGHT = 8;
        System.out.println("Calculation of Paint Requirements");
        length = promptDouble("Enter room length: ");
        width = promptDouble("Enter room width: ");
        wallArea = perimeter(length, width) * HEIGHT;
        ceilingArea = area(length, width);
        System.out.println("The wall area is " + wallArea + " square feet.");
        System.out.println("The ceiling area is " + ceilingArea + " square feet.");
    }
}


Related Solutions

Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
In the second task, you will write a Java program that validates an input to see...
In the second task, you will write a Java program that validates an input to see whether it matches a specified pattern. You will have to read a bit about regular expressions for this Lab. 1. Prompt the user to enter the email address. 2. Read the user input as a String. 3. A regular expression that specifies the pattern for an email address is given by “[a-zA-Z0- 9.]++@[a-zA-Z]++.(com|ca|biz)”. Use this as the input to the matches method in the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78. Instructions Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
Must be written in Java: After completing this lab, you should be able to: Write a...
Must be written in Java: After completing this lab, you should be able to: Write a Java class to represent Time. Create default and parameterized constructors. Create accessor and mutator methods. Write a toString method. This project will represent time in hours, minutes, and seconds. Part 1: Create a new Java class named Time that has the following instance fields in the parameterized constructor: hours minutes seconds Create a default constructor that sets the time to that would represent the...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional array to store how many pounds of food three monkeys eats each day in a week. The 2D array is then passed to functions to find total, average and least amount of food consumption, etc. The program then need to display: the average amount of food the three monkeys ate per day the least amount of food eaten all week by any one monkey....
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus...
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus and prints the employee’s name and bonus. Bonuses are calculated based on an employee’s productivity score as shown below. A productivity score is calculated by first dividing an employee’s transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked. Productivity Score Bonus <=30 $50 31–69 $75 70–199 $100 >= 200 $200 Instructions Ensure the file...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions Ensure the file named Multiply.java is open. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize...
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory...
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory function (¬?) 2) logical and (? ∧ ?) 3) logical inclusive or (? ∨ ?) 4) implication function (? → ?) 5) biconditional (? ↔ ?) 6) logical exclusive or (? ⨁ ?) You can find the logical definitions for the above functions as given by Bertrand Russell in your text. You need to write a method (function) for each corresponding logical function. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT