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

--- --------- 1. Add a new method to your class called print() The print() metho

ID: 3919406 • Letter: #

Question

---

---------

1. Add a new method to your class called print()

The print() method is similar to the display method, but it will print the data to a file called "mydata.txt" rather then to the screen [1 point]

The file should append new data every time the print() method is called, and not replace it [1 point]

2. Create a subclass

Create a sub class that inherits everything from your base class. (For example, if Car is the base class then SportsCar could be your sub class) [1 point]

Provide at least one additional attribute to your subclass [1 point]

Create gettter/setter methods for it.

Create a default constructor for the subclass, that uses super to call the base class default constructor. [1 point]

It should set all attributes in the subclass as well as the super class to default values

Create a parameterized constructor for the subclass, that uses keyword super to pass the inherited parameters to the base class. [1 point]

It should set all attributes in the subclass as well as the super class to the values that are passed in to the constructor.

Override the print() method to print out (to the file "mydata.txt") all the instance variable values from the base class, and also from the sub class. [1 point]

3. Update main method

In your main method, create 2 new object using your subclass [1 point]
Create one object using the no-arg (default) constructor of your sub-class.

Call the set methods to set all attribute data associated to that object.

Create one object using the parameterized constructor of your sub-class.

Create an ArrayList containing these 2 objects, and also the 2 objects from your BA5 assignment to have at least 4 total objects inside it. [1 point]

Loop through the ArrayList using a for loop and call the print() method for each object. [1 point]

Extra Credit (+1 point): Create a Java Interface (see Chapter 12) called Displayable that declares the display() and print() methods. Both your class and subclass should implement the interface.

Explanation / Answer

Sushi.java

import java.io.FileWriter;

import java.io.IOException;

import java.io.File;

public class Sushi {

private double price;

private String sushiName;

private int orderNumber;

public Sushi() {

}

public Sushi(int orderNumber, double price, String sushiName) {

super();

this.orderNumber = orderNumber;

this.price = price;

this.sushiName = sushiName;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public String getName() {

return sushiName;

}

public void setName(String sushiName) {

this.sushiName = sushiName;

}

public int getOrder() {

return orderNumber;

}

public void setOrder(int orderNumber) {

this.orderNumber = orderNumber;

}

public void display() {

System.out.println("Sushi Price: $" + price);

System.out.println("Sushi Name: " + sushiName);

System.out.println("Order Number: " + orderNumber);

}

public void print() throws IOException {

File f = null;

FileWriter fw;

// Writing the data to the file

try {

f = new File("mydata.txt");

// If file already exists then write data to the existing

// file

if (f.exists()) {

fw = new FileWriter(f, true);

fw.write("Sushi Price: $" + price+" ");

fw.write("Sushi Name: " + sushiName+" ");

fw.write("Order Number: " + orderNumber+" ");

} else {

// If no file already exists.Create new File

f.createNewFile();

fw = new FileWriter(f);

fw.write("Sushi Price: $" + price+" ");

fw.write("Sushi Name: " + sushiName+" ");

fw.write("Order Number: " + orderNumber+" ");

}

fw.close();

} catch (Exception e) {

System.out.println("Exception " + e);

}

}

}

______________

SushiRoll.java

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

public class SushiRoll extends Sushi {

private String ingedient;

public SushiRoll() {

super();

}

public SushiRoll(int orderNumber, double price, String sushiName,

String ingedient) {

super(orderNumber, price, sushiName);

this.ingedient = ingedient;

}

public String getIngedient() {

return ingedient;

}

public void setIngedient(String ingedient) {

this.ingedient = ingedient;

}

@Override

public void print() throws IOException {

super.print();

File f = null;

FileWriter fw;

// Writing the data to the file

try {

f = new File("mydata.txt");

// If file already exists then write data to the existing

// file

if (f.exists()) {

fw = new FileWriter(f, true);

fw.write("Ingredient: " + ingedient+" ");

} else {

// If no file already exists.Create new File

f.createNewFile();

fw = new FileWriter(f);

fw.write("Ingredient: " + ingedient+" ");

}

fw.close();

} catch (Exception e) {

System.out.println("Exception " + e);

}

}

}

_________________

Demo.java

import java.io.IOException;

import java.util.ArrayList;

public class Demo {

public static void main(String[] args) {

Sushi p1 = new Sushi();

p1.setPrice(10.5);

p1.setName("Salmon");

p1.setOrder(1);

  

Sushi p2 = new Sushi(2, 12.50, "Tuna");

  

SushiRoll s1=new SushiRoll();

s1.setPrice(11.50);

s1.setName("Trout");

s1.setOrder(3);

s1.setIngedient("Rice");

SushiRoll s2=new SushiRoll(4,12.50,"Marlin","Wheat");

ArrayList<Sushi> arl=new ArrayList<Sushi>();

arl.add(p1);

arl.add(p2);

arl.add(s1);

arl.add(s2);

  

for(int i=0;i<arl.size();i++)

{

try {

arl.get(i).print();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

_________________

Output file:

mydata.txt

Sushi Price: $10.5
Sushi Name: Salmon
Order Number: 1

Sushi Price: $12.5
Sushi Name: Tuna
Order Number: 2

Sushi Price: $11.5
Sushi Name: Trout
Order Number: 3

Ingredient: Rice

Sushi Price: $12.5
Sushi Name: Marlin
Order Number: 4

Ingredient: Wheat

___________Thank You