Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) State if the following are valid identifiers or not. If not, give your reaso

ID: 3698409 • Letter: #

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

(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.

Explanation / Answer

(a)

(i) class ---- invalid, class is a keyword

(ii) 3c --- invalid, identifier cannot start with a digit

(iii) easy2Learn ---- valid

(iv) p-q ---- invalid, - sign is not allowed in identifier name

(v) thanks ---- valid

(b)

(i) 33f --- String

(ii) "8" --- String

(iii) 's' ---- char

(iv) false ---- boolean

(v) 34567890 --- long

(c)

(i) 4 * 3 * 2.2 / 2 = 12 *1.1 = 13.2

(ii) 7 / 2 * 2 * 1.1 = 3*2*1.1 = 6.6

(iii) (5 + 3) / 3 * 4 [3] --- Invalid, 4[3] is invalid

(d)

i)

class Bread  
{
private String name;
private double price;
?}

ii)

class TestBread
{
public static void main (String[] args)
{

Bread breadA = new Bread();
System.out.println("An object breadA of class Bread has been created");

}

iii)

public void setPrice(double price)
{
  this.price = price;
}

iv)

public String getName()
{
return name;
}

v)

public void increasePrice(double amount)
{
  price = price + amount;
}

vi)

breadA.setName("Tuner fish bread");
breadA.setPrice (8.5);
breadA.increasePrice(2);

System.out.println("Current Price of breadA : "+breadA.getPrice());

------------------------------------------

class Bread  
{
private String name;
private double price;

public void setPrice(double price)
{
  this.price = price;
}
public void setName(String name)
{
  this.name = name;
}

public String getName()
{
return name;
}
public double getPrice()
{
  return price;
}
public void increasePrice(double amount)
{
  price = price + amount;
}
}
class TestBread
{
public static void main (String[] args)
{

Bread breadA = new Bread();
System.out.println("An object breadA of class Bread has been created");

breadA.setName("Tuner fish bread");
breadA.setPrice (8.5);
breadA.increasePrice(2);

System.out.println("Current Price of breadA : "+breadA.getPrice());

}
}

Output:

An object breadA of class Bread has been created
Current Price of breadA : 10.5

Do ask if any doubt. Please upvote.