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

Part 1 Create an Interface called Printable, that defines 3 methods: toString()

ID: 3607252 • Letter: P

Question

Part 1

Create an Interface called Printable, that defines 3 methods: toString() and display() and printToFile() [10 points]

Update your class from your previous assignments to have the following

Implement the Comparable Interface [5 points]

Override to compareTo() method - (I'ts up to you to determine the best way to sort your objects) [20 points]

Implement the Printable interface - You will also need to implement all the methods, some of which you should already have. The printToFile() method should just simply print your data to a text file.[10 points]

In the driver class (the class with your main method), define your own generic static method called maxHighest()

It should have 3 generic parameters [10 points]

It will return the max highest item (based on their sorting order) of the 3 parameters [10 points]

In your main method, test the maxHighest() method by passing the following objects to it. It should print out the some text describing it as the 2nd highest item:

3 Strings  [5 points]

3 Integers [5 points]

3 Objects of your Class from your previous assignments  [5 points]


Previous assignment:

public class Coffee {

   protected String color;

   protected double size;

   protected int price;

   // default constructor

   public Coffee() {

       color = " ";

       size = 0;

       price = 0;

   }

   // parameterized constructor

   public Coffee(String c, double s, int p) {

       color = c;

       size = s;

       price = p;

   }

   public void setColor(String c) { // Can right click to generate get and set methods, under source

       color = c;

   }

   public void setSize(double s) {

       size = s;

   }

   public void setPrice(int p) {

       price = p;

   }

   public String getColor() {

       return color;

   }

   public double getSize() {

       return size;

   }

   public int getPrice() {

       return price;

   }

   public void display() {

       System.out.println(color);

       System.out.println(size + " inches");

       System.out.println("$" + price);

       //System.out.print(" ");

   }

}

public class Starbucks extends Coffee {

   private boolean isDecaf;

   public Starbucks() {

       super("", 0, 0);

       this.isDecaf = false;   

   }

   public Starbucks(boolean isDecaf, String color, double size, int memory) {

       super(color, size, memory);

       this.isDecaf = isDecaf;   

   }   

   public void setisDecaf(boolean isDecaf)

   {

       this.isDecaf = isDecaf;

   }   

   public boolean getisDecaf() {

       return isDecaf;

   }

   public void display() {

       super.display();

       System.out.println("and it is decaf: " + isDecaf);

   }

}

import java.util.ArrayList;

public class Demo {

   public static void main(String[] args) {

       Coffee a = new Coffee();

       Coffee b = new Coffee("Black", 6, 4);

       Starbucks c = new Starbucks();

       Starbucks d = new Starbucks(true, "Black", 6, 4);

      

       a.setColor("Black");

       a.setSize(6);

       a.setPrice(4);

      

       c.setColor("Black");

       c.setSize(8);

       c.setPrice(6);

       c.setisDecaf(false);

       ArrayList<Coffee> arr = new ArrayList<Coffee>();

       arr.add(a);

       arr.add(b);

       arr.add(c);

       arr.add(d);

       displayAll(arr);

   }

       public static void displayAll(ArrayList<Coffee> Starbucks) {

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

               Starbucks.get(i).display();

           }

       }

   }

Explanation / Answer

import java.io.FileNotFoundException;

import java.io.IOException;

public interface Printable {

String toString();

void display();

void printToFile(String Filename) throws FileNotFoundException, IOException;

}

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class Coffee implements Comparable<Coffee>,Printable{

protected String color;

protected double size;

protected int price;

// default constructor

public Coffee() {

color = " ";

size = 0;

price = 0;

}

// parameterized constructor

public Coffee(String c, double s, int p) {

color = c;

size = s;

price = p;

}

public void setColor(String c) { // Can right click to generate get and set methods, under source

color = c;

}

public void setSize(double s) {

size = s;

}

public void setPrice(int p) {

price = p;

}

public String getColor() {

return color;

}

public double getSize() {

return size;

}

public int getPrice() {

return price;

}

public void display() {

System.out.println(color);

System.out.println(size + " inches");

System.out.println("$" + price);

//System.out.print(" ");

}

  

@Override

public int compareTo(Coffee o) {

return this.price-o.price;

}

FileOutputStream out = null;

@Override

public void printToFile(String Filename) throws IOException{

out = new FileOutputStream(Filename);

byte[] ColorToBytes = color.getBytes();

out.write(ColorToBytes);

out.write((int) size);

out.write(price);

}

@Override

public String toString(){

String s;

s = color + " " + size + " inches $"+price+" ";

return s;

}

}

import java.util.ArrayList;

import java.util.Arrays;

public class Demo {

public static void main(String[] args) {

Coffee a = new Coffee();

Coffee b = new Coffee("Black", 6, 4);

Starbucks c = new Starbucks();

Starbucks d = new Starbucks(true, "Black", 6, 4);

  

a.setColor("Black");

a.setSize(6);

a.setPrice(4);

  

c.setColor("Black");

c.setSize(8);

c.setPrice(6);

c.setisDecaf(false);

maxHighest(1, 2, 3);

maxHighest("Hello","World", "Java");

maxHighest(a,b,c);

ArrayList<Coffee> arr = new ArrayList<Coffee>();

arr.add(a);

arr.add(b);

arr.add(c);

arr.add(d);

displayAll(arr);

}

public static void displayAll(ArrayList<Coffee> Starbucks) {

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

Starbucks.get(i).display();

}

}

  

public static Object maxHighest(Object o1,Object o2,Object o3){

if(o1 instanceof Integer){

Integer a,b,c;

a = (Integer) o1;

b = (Integer) o2;

c = (Integer) o3;

int arr[] = new int[3];

arr[0] = a;

arr[1] = b;

arr[2] = c;

Arrays.sort(arr);

System.out.println("Second maximum element is " + arr[1]);

return arr[2];

}else if( o1 instanceof String){

String a,b,c;

a = (String) o1;

b = (String) o2;

c = (String) o3;

String arr[] = new String[3];

arr[0] = a;

arr[1] = b;

arr[2] = c;

Arrays.sort(arr);

System.out.println("Second maximum element is " + arr[1]);

return arr[2];

}else{

Coffee a,b,c;

a = (Coffee) o1;

b = (Coffee) o2;

c = (Coffee) o3;

Coffee arr[] = new Coffee[3];

arr[0] = a;

arr[1] = b;

arr[2] = c;

Arrays.sort(arr);

System.out.println("Second maximum element is " + arr[1]);

return arr[2];

}

}

}

public class Starbucks extends Coffee {
private boolean isDecaf;
public Starbucks() {
super("", 0, 0);
this.isDecaf = false;
}  
public Starbucks(boolean isDecaf, String color, double size, int memory) {
super(color, size, memory);
this.isDecaf = isDecaf;
}
public void setisDecaf(boolean isDecaf)
{
this.isDecaf = isDecaf;
}
public boolean getisDecaf() {
return isDecaf;
}
public void display() {
super.display();
System.out.println("and it is decaf: " + isDecaf);
}
}