Specialcolor

 avatar
unknown
java
3 years ago
1.4 kB
13
Indexable
package p2;
import java.util.*;
public class midterm2task2 {

	public static void main(String[] args) {
		Banana banana = new Banana(30,"Yellow");
		banana.print();
		
		Melon melon = new Melon(12,30);
		melon.print();
		
		Watermelon watermelon = new Watermelon(90, 12);
		watermelon.print();
		System.out.println(watermelon.returnWeight());
	}

}
abstract class Fruit{
	 double sweetness;
	 public abstract void print();
	 Fruit(double sweetness){
		 this.sweetness=sweetness;
	 }
 }
 
class Banana extends Fruit{
	String color;
	Banana(double sweetness, String color){
		super(sweetness);
		this.color=color;
	}
	public void print() {
		System.out.println("The banana is " + color + " and has " + sweetness + " % sweetness. ");
	}
	}
class Melon extends Fruit{
	double weight;
	Melon(double sweetness, double weight){
		super(sweetness);
		this.weight=weight;
	}
	public void print() {
		System.out.println("The melon weights " + weight + " and has " + sweetness + " % sweetness. ");
	}
}
 class Watermelon extends Melon{
	 Watermelon(double sweetness, double weight){
		 super(sweetness, weight);
		 
	 }
	 public void print() {
		System.out.println("The watermelon weights " + weight + " and has " + sweetness + " % sweetness. ");
	 	}
	 public double returnWeight() {
		 return weight;
	 }
 }
 
Editor is loading...