Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can somebody please help me with these two java problems? I need help! 1. This Lab exercise is worth 3 pointsFor the second part of

Can somebody please help me with these two java problems? I need help!

1.

This Lab exercise is worth 3 pointsFor 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 repeated and think about how to write 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.java source file for you to modify ( lines 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 might make a nice function, promptDouble:

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

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

width = Keyboard.nextDouble();

// these 3 lines could use the same function:

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

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

// this line might use a perimeter function, like so:

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

ceilingArea = length * width;

// this line can provide/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 changes in a Java program called Painting.java. This Lab exercise is worth 6 points.

2. In Java a function can call itself (we may explore more about this later in the course). This is called a recursive function. In order for this to work there must be a situation where the function finally stops calling itself and simply returns a value (or just returns). Without giving a lot of information now, we can use a Java if statement to make a recursive function return as required. This special case where the function finally returns is called the recursive function base case. Here is a simple example: a function to calculate the factorial of an integer number n >= 1. Remember that the factorial of n, n!, is n(n-1)(n-2)1 if n is greater than or equal to 1. The base case for this function occurs when n is 1, and at that point the function should return 1; otherwise the function returns n(n-1)! by definition, n! is n(n-1)! if n > 1. Heres a Java recursive function factorial implementation, assuming n is at least 1: public static long factorial(int n)

// recursive call factorial calls itself! // this second return executes if the if condition is false } Your job for this part of the Lab is to create and test a similar recursive function that calculates and returns an integer power p of some integer number n; n and p are the parameters to the function, with header line static long power(int n, int p). You can assume that p is at least 0. Modify the above function as follows:

Update the header line for the function and then make these changes: The base case for the power function occurs when p is 0, at which point the function returns the value 1 (by definition, any number raised to the 0 power is 1). To test for 0, the base case, replace the if condition with this one: if (p == 0).

If p is not 0 then the function should return n*power(n, p-1) np is nn(p-1) if p is greater than 0. Use that information to change the second return statement.

Now test to make sure power works by having main call it with some known values and print out the results. For example, power(3, 2) should return 32 = 9, power(-2, 7) should return (-2)7 = -128, and power(8, 0) should return 1.

Derive Power.java from Factorial.java, and modify main to test power.image text in transcribedimage text in transcribed

This Lab exercise is worth 3 pointsFor 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 repeated and think about how to write 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.java source file for you to modify (blue lines 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( ) ; /7these 2 1ines might make a nice function, // length-promptDouble(" Enter room length: "); System.out.print(" Enter room width: "); width = // these 3 lines could use the same function: // width-prountDouble(" Enter room width: "); wallArea-2 * (length + width) * HEIGHT; // ignore doors // this line might use a perimeter function, like so: // wallAcea= perimeter(length, width) * HEIGHT; ceilingArea= length * width; // this line can provide/use a new function called area, similar to perimeter, 1ike so: 7I seilingacea 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 changes in a Java program called Painting.java. This Lab exercise is worth 6 points

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions