Question
(a) Determine whether the following relational expressions are valid or invalid in Java. Provide the expression results for valid expressions. If the expression is invalid,
(a) Determine whether the following relational expressions are valid or invalid in Java. Provide the expression results for valid expressions. If the expression is invalid, provide explanations.
(i) 5 < 4 + 1
(ii) false >= true
(iii) "Hello" == "hello"
(iv) 0.7 != 0.8 - 0.1
(v) e > e - 0 (where e is an initialized non-local variable of integer type) (b) Variables c and d are of type double in the following statement:
c = d = 2.5;
Is it a valid statement in Java? If yes, what will it do? If no, please explain why it is invalid.. (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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started