Question

Write a method printCostTable() that prints out a table like the following: price carpet fitting 5.0...

Write a method printCostTable() that prints out a table like the following:

price carpet fitting
5.0 150.0 120.0
9.0 270.0 120.0
13.0 390.0 120.0

The method should take four int arguments representing the width and length of the carpet, a start price and an end price.

The method should first print the headings

price carpet fitting

It should then print the price, carpet cost and fitting cost for each price starting from startPrice and increasing in increments of £4, up to but not exceeding endPrice. For example, assume you have initialised an instance of CarpetCostEstimator cce with labour charge £4.0. Then if startPrice is £5 and endPrice £15, and you execute.

cce.printCostTable(5, 6, 5, 15);

three rows of figures should be printed, as shown in the example above.

In order to align the columns of the table accurately, you should begin your method by defining a string that represents a tab character, like this

String tab = "\t";

I'm not sure on how to start with this java exercise so that the function would print a table.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Full working Java code:

import java.io.*;
class Demo {
//Static method to print the desired table
public static void printCostTable(int width, int length, int startPrice, int endPrice)
{
//Calculate the area
float area = width * length;
//Fitting cost is fixed
float fittingCost = 120.0f;
String tab = "\t";
//Print the header first
System.out.println("price"+tab+"carpet"+tab+"fitting");
//Run loop till the startPrice becomes just less than end price and increase startprice by 4
for(float i = startPrice; i < endPrice ; i+=4)
{
//Display the row according to given format
System.out.println(i+tab+(area*i)+tab+fittingCost);
}
}
   public static void main (String[] args) {
   //Call the static method with arguments.
       printCostTable(5, 6, 5, 15);
   }
}

Output:

price   carpet  fitting
5.0         150.0       120.0
9.0         270.0       120.0
13.0    390.0   120.0
Add a comment
Know the answer?
Add Answer to:
Write a method printCostTable() that prints out a table like the following: price carpet fitting 5.0...
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
  • Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows...

    Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows in a graphical way, as shown below, the names stored in a file. The coordinates to print the names will be also read from another file. Here is an example of what your graphic output should look like with the two files that are provided for test purposes. Drawing Panel Adam Bernie Cameron Daniel Fanny Gerard Harold Issac Jackie Raitlyn Note that the colors...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array 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...

  • Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...

    Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originally wrote this assignment (for their CSE 142, in Java) This program focuses on classes and objects. Turn in two files named Birthday.cs and Date.cs. You will also need the support file Date.dll; it is contained in the starter project for this assignment. The assignment has two parts: a client program that uses Date objects, and a...

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
ADVERTISEMENT