Complete the following steps using either the class you created in BA5, do the f
ID: 3919068 • Letter: C
Question
Complete the following steps using either the class you created in BA5, do the following:
1. Add a new method to your class from BA5 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.
here is my BA5?
public class Pizza {
private int pizzaStyle;
private double price;
private String title;
public Pizza() {
}
public Pizza(int pizzaStyle, double price, String title) {
super();
this.pizzaStyle = pizzaStyle;
this.price = price;
this.title = title;
}
public void display() {
System.out.println("Pizza (Price= "+price+", Size= "+title+",)");
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPizzaStyle() {
return pizzaStyle;
}
public void setPizzaStyle(int pizzaStyle) {
this.pizzaStyle = pizzaStyle;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
------- and here is the demo class to print
public class Demo {
public static void main(String[] args) {
Pizza p1 = new Pizza();
p1.setPrice(7.99);
p1.setTitle("Small");
p1.setPizzaStyle(1);
p1.display();
Pizza p2 = new Pizza(2, 12.99, "Medium");
p2.display();
}
}
Explanation / Answer
here is your classes : ------------->>>>>>>
Pizza.java : ---------------->>>>>>>>>>>>>
import java.io.PrintWriter;
import java.io.File;
import java.io.FileOutputStream;
public class Pizza implements Displayable{
private int pizzaStyle;
private double price;
private String title;
public Pizza() {
}
public Pizza(int pizzaStyle, double price, String title) {
super();
this.pizzaStyle = pizzaStyle;
this.price = price;
this.title = title;
}
public void display() {
System.out.println("Pizza (Price= "+price+", Size= "+title+",)");
}
public void print(){
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("mydata.txt"),true));
pw.println("Pizza (Price= "+price+", Size= "+title+",)");
}catch(Exception e){
e.printStackTrace();
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPizzaStyle() {
return pizzaStyle;
}
public void setPizzaStyle(int pizzaStyle) {
this.pizzaStyle = pizzaStyle;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
VegPizza.java : --------------->>>>>>>>>>>>>>
import java.io.PrintWriter;
import java.io.File;
import java.io.FileOutputStream;
public class VegPizza extends Pizza implements Displayable{
private boolean extraCheese;
public VegPizza(){
super();
extraCheese = false;
}
public VegPizza(int pizzaStyle, double price, String title,boolean extraCheese){
super(pizzaStyle,price,title);
setExtraCheese(extraCheese);
}
public boolean getExtraCheese(){
return extraCheese;
}
public void setExtraCheese(boolean extra){
extraCheese = extra;
}
public void display() {
if(extraCheese){
System.out.println("Pizza (Price= "+getPrice()+", Size= "+getTitle()+",, With Extra Cheese"+")");
}else{
System.out.println("Pizza (Price= "+getPrice()+", Size= "+getTitle()+", Without Extra Cheese"+")");
}
}
public void print(){
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("mydata.txt"),true));
if(extraCheese){
pw.println("Pizza (Price= "+getPrice()+", Size= "+getTitle()+",, With Extra Cheese"+")");
}else{
pw.println("Pizza (Price= "+getPrice()+", Size= "+getTitle()+",, Without Extra Cheese"+")");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Demo.java : ----------------------->>>>>>>>>
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
VegPizza p1 = new VegPizza();
VegPizza p2 = new VegPizza(2,234.0,"Any Name",false);
p1.setPrice(346.78);
p1.setTitle("Any Name 2");
p1.setPizzaStyle(3);
p1.setExtraCheese(true);
ArrayList<Pizza> pizzas = new ArrayList<>();
pizzas.add(p1);
pizzas.add(p2);
pizzas.add(new Pizza());
pizzas.add(new Pizza(1,200.98,"Any name 3"));
for(int i = 0;i<pizzas.size();i++){
pizzas.get(i).display();
pizzas.get(i).print();
}
}
}
Displayable.java : ------------->>>>>>>>
public interface Displayable{
public void display();
public void print();
}