Hi I need help with these two java programs 1. Write a class named blend. The bl
ID: 3572876 • Letter: H
Question
Hi I need help with these two java programs
1. Write a class named blend. The blend class should contain me the quantity of the total coffee in the blend (in pounds), the time of the roast (in hours use a double) of the blend, the name of the blend, and predicted yield in gallons of the blend. Each blend may contain up to three different coffees and will contain the pounds of each used. The blend should have a constructor and applicable methods and be able to display each of the coffees used in the blend via the methods called in any instance. (You should use aggregation in this class)
2. Write small program that has four classes, Vehicle, Car, Truck, motorcycle. Vehicle hold the Vin number (String) Weight. The remaining classes extend Vehicle. Car holds number of doors, number of passengers, Truck holds cargo weight, and motorcycle holds wheel size. In the main use polymorphism to fill an array of vehicles with each of the noted vehicles then display the Vin numbers of each. Hard code the data input and use a simple loop with System.out.println for your output.
Explanation / Answer
Answer For First Program is
public class Blend {
int quantity,yeild;
double time;
String name;
public Blend(int quantity,double time,String name){
this.name= name;
this.quantity=quantity;
this.time=time;
}
void display(){
System.out.println("Quantity "+quantity+" Time "+time+"Hrs Name "+name);
}
public static void main(String[] args) {
Blend a = new Blend(450,20,"Coffee A");
Blend b = new Blend(462,74,"Coffee B");
Blend c = new Blend(500,10,"Coffee C");
a.display();
b.display();
c.display();
}
}
Answer For Second Program is
class Vehicle {
String vin="5555";
int weight=420;
void spec(){
}
}
class Car extends Vehicle {
int noDoors=5,npassg=10;
String vin = "56465";
void spec(){
System.out.println("Vin For Car is "+vin);
}
}
class Truck extends Vehicle{
int cweight;
String vin = "75628";
void spec(){
System.out.println("Vin For Truck is "+vin);
}
}
class Motorcycle extends Vehicle{
int size=10;
String vin = "79994";
void spec(){
System.out.println("Vin For Motorcycle is "+vin);
}
public static void main(String[] args){
Vehicle[] V = new Vehicle[3];
V[0]=new Car();
V[1]=new Truck();
V[2]=new Motorcycle();
V[0].spec();
V[1].spec();
V[2].spec();
}
}
If you put aboove second code in Program name Motorcycle.java then it will run properly. Beacuse main method in Motorcycle class.
Still I am changing the second program for your better understanding.
Create Two different class in same package.
Vehicle.java
public class Vehicle {
String vin;
int weight=420;
void spec(){
System.out.println("I am in vehicle class");
}
public static void main(String[] args) {
Vehicle v = new Vehicle();
v.spec();
}
}
Motorcycle.java
class Car extends Vehicle {
int noDoors=5,npassg=10;
void spec(){
}
}
class Truck extends Vehicle{
int cweight;
void spec(){
}
}
class Motorcycle extends Vehicle{
int size=10;
void spec(){
}
public static void main(String[] args){
Vehicle[] V = new Vehicle[3];
V[0]=new Car();
V[1]=new Truck();
V[2]=new Motorcycle();
//Instance of class Vehicle is created here
Vehicle s = new Vehicle();
s.spec(); //sepc() method of vehicle class will run here
V[0].vin="7958222";
V[1].vin="7958766";
V[2].vin="7513576";
for(int i=0;i<V.length;i++){
if(i==0){
System.out.print("VIN For Car is ");
}else if(i==1)
{
System.out.print("VIN For Truck is ");
}
else if(i==2){
System.out.print("VIN For Motorcycle is ");
}
System.out.println(V[i].vin);
}
}
}
And run the Motorcycle.java program.
You will get output.
Thank you...!