Question
(a) State if the following are valid identifiers or not. If not, give your reason(s) in a few words. (i) class (ii) 3c (iii) easy2Learn
(a) State if the following are valid identifiers or not. If not, give your reason(s) in a few words.
(i) class
(ii) 3c
(iii) easy2Learn
(iv) p-q
(v) thanks
(b) Write down the most suitable data types for the following data:
(i) 33f
(ii) "8"
(iii) 's'
(iv) false
(v) 34567890
(c) Determine the results of the following expressions in Java. Explain each result in one or two sentences.
(i) 4 * 3 * 2.2 / 2
(ii) 7 / 2 * 2 * 1.1
(iii) (5 + 3) / 3 * 4 [3]
(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
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