Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(c) Using the class Bread in part (d) of Question 2, perform the following: (i) Write a method isCheap() which does not have any parameters

(c) Using the class Bread in part (d) of Question 2, perform the following:

(i) Write a method isCheap() which does not have any parameters and returns true if the price is less than 4 and false otherwise. Copy the content of the method as the answers to this part.

(ii) Write a method category() which returns the category of the bread as a string using a switchcase statement. The category of the bread is determined by the following table.

Category price
"Expensive" not less than 8
"Medium" 4 to less than 8
"Cheap" less than 4

Copy the content of the method as the answers to this part.

(iii) Write a method generateBar(char ch) which prints price and a simple graph formed by ch. One character ch is printed for every 1 dollar and rounding is done on the remaining part. For example, the call aBread.generateBar('*') generate the following output:

3.8 ****

where aBread is an object of Bread with price being 3.8. You are required to use a for loop to generate the bar. Copy the content of the method as the answers to this part.

(iv) Create another class TestBread2 with a method main() to test the new methods in class Bread. You need to perform the tasks:

create a bread object breadB;

set the price to 3.8 using the method setPrice() and set the name to "Wheat bread" using an appropriate method;

make use of the methods getName() and isCheap(), print the name and the result of calling isCheap();

make use of the method category(), print the category of breadB;

use the method generateBar() to print a simple graph of for breadB using "*";

create another bread object breadC.

Run the program. Copy the content of the class and the output as the answers to this part.

The information of part d of Question 2 :

(d) The following is about creating a class Bread and testing it. In every part, correct any syntax errors indicated by NetBeans until no such error messages.

(i) Create a class Bread with the attributes name and price. The attributes are used to store the name and the price of the bread respectively. Choose suitable types for them. You can copy the class Counter on p.17 of the unit and modify the content. Copy the content of the file as the answers to this part. No screen dump is recommended to minimize the file size.

(ii) Create another class TestBread with a method main() to test the class Bread. You can copy the class TestCounter on p.42 and modify the content. In main(), create a bread object breadA and print the message "An object breadA of class Bread has been created". Run the program. Copy the content of the file and the output showing the message as the answers to this part.

(iii) Add a method setPrice() to the Bread class with appropriate parameter(s) and return type to set the price of the bread. Copy the content of the method as the answers to this part.

(iv) Add another method getName() to the Bread class with appropriate parameter(s) and return type to get the name of the bread. Copy the content of the method as the answers to this part. You should create other setter/getter methods in your class file but no marks are allocated for them since they are similar to the ones here and in part (iii).

(v) Write another method increasePrice(double amount) of the Bread class which increases the price by amount. Copy the content of the method as the answers to this part.

(vi) In the class TestBread, add the following before the end of main():

set the name of the bread object to "Tuner fish bread" using the method setName();

set the price to 8.5 using the method setPrice();

increase the price by 2 using the method increasePrice();

print the current price after getting it using the method getPrice(); Run the program. Copy the content of the class and the output as the answers to this part.

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

More Books

Students also viewed these Databases questions

Question

Identify the universal properties of all languages.

Answered: 1 week ago