Using java, think about the relationships among all these classes (Car companies
ID: 3682565 • Letter: U
Question
Using java, think about the relationships among all these classes (Car companies, models, cars, miles per gallon...). What methods should be common to each, and which should be different. In a hierarchy, the top classes should be more broad while child classes introduce methods and variables more specific to it. You will want to demonstrate this, rather than just having generic println methods. Could you please add methods to demonstrate the connections?
/* Aidan Acosta
* 2/20/16
* Class Hierarchy
*/
public class Ford
{
int companyValue = 0;
public static int methodFord()
{
System.out.println("Ford Motor Company");
return 1;
}
}
class Lincoln extends Ford
{
int staffNum = 0;
public String methodLincoln()
{
System.out.println("Lincoln: Car company within Ford");
return "2";
}
}
class SUV extends Lincoln
{
int numberOfModels = 0;
public String methodSUV()
{
System.out.println("SUV: Car type within Lincoln");
return "3";
}
}
class Navigator extends SUV
{
int weight = 0;
public static String methodNavigator()
{
System.out.println("Navigator: Model within car type");
return "4";
}
}
class MPG extends Navigator
{
int mpg = 0;
public static String methodMPG()
{
System.out.println("MPG: Characteristic of a model");
return "5";
}
}
/*
* Aidan Acosta
* 2/20/16
* Hierarchy Driver
*/
public class Driver
{
public static void main(String args[])
{
try
{
Navigator e = new Navigator();
e.methodFord();
e.methodLincoln();
e.methodSUV();
e.methodNavigator();
MPG.methodMPG();
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
Explanation / Answer
public class Ford
{
int companyValue = 0;
public static int methodFord()
{
System.out.println("Ford Motor Company");
return 1;
}
}
class Lincoln extends Ford
{
int staffNum = 0;
public String methodLincoln()
{
System.out.println("Lincoln: Car company within Ford");
return "2";
}
}
class SUV extends Lincoln
{
int numberOfModels = 0;
public String methodSUV()
{
System.out.println("SUV: Car type within Lincoln");
return "3";
}
}
class Navigator extends SUV
{
int weight = 0;
public static String methodNavigator()
{
System.out.println("Navigator: Model within car type");
return "4";
}
}
class MPG extends Navigator
{
int mpg = 0;
public static String methodMPG()
{
System.out.println("MPG: Characteristic of a model");
return "5";
}
class Security extends MPG
{
String security="Central Locking";
public static String methodsecurity()
{
System.out.println("Security:Central Locking");
return "6";
}
}
class Safety extends Security
{
int Airbags=6;
public static String methodsafety()
{
System.out.println("6 airbags");
return "7";
}
}
class Comfort extends Safety
{
String Air_conditioner="Automatic Climate Control";
public static String methodair()
{
System.out.println("Automatic Climate Control");
return "8";
}
public void method()
{
Comfort c=new Comfort();
c.methodFord();
c.methodLincoln();
c.methodSUV();
c.methodNavigator();
c.methodMPG();
c.methodair();
c.methodsafety();
c.methodsecurity();
}
}
public class Driver
{
public static void main(String args[])
{
try
{
Comfort e = new Comfort();
e.method();
}
catch (Exception ex)
{
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.