Question

In: Computer Science

Define an interface called Vehicle, which requires the following methods getName: returns a String getTopSpeed: returns...

Define an interface called Vehicle, which requires the following methods getName: returns a String getTopSpeed: returns an int getMaxPassengers: returns an int Note that the class Car implements this interface and is preloaded (in one of the following questions you will define this class)

For example:

Test

Vehicle v = new Car("BMW", 280, 5); System.out.println(v.getName()); System.out.println(v.getTopSpeed()); System.out.println(v.getMaxPassengers());

Results :

BMW

280

5

Test 2 :

Vehicle v = new Car("Smart", 150, 2); System.out.println(v.getName()); System.out.println(v.getTopSpeed()); System.out.println(v.getMaxPassengers());

Results :

Smart

150

2

Solutions

Expert Solution

//Test.java

/* Interface Vehicle with abstract methods */
interface Vehicle {
   public String getName();

   public int getTopSpeed();

   public int getMaxPassengers();
}

/* class car implements interface Vehicle */
class Car implements Vehicle {
   private String name;
   private int topSpeed, maxPassengers;

   public Car(String string, int i, int j) {
       setName(string);
       setTopSpeed(i);
       setMaxPassengers(j);
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getTopSpeed() {
       return topSpeed;
   }

   public void setTopSpeed(int topSpeed) {
       this.topSpeed = topSpeed;
   }

   public int getMaxPassengers() {
       return maxPassengers;
   }

   public void setMaxPassengers(int maxPassengers) {
       this.maxPassengers = maxPassengers;
   }
}

/* Test class */
public class Test {
   public static void main(String[] args) {
       Vehicle v = new Car("BMW", 280, 5);
       System.out.println(v.getName());
       System.out.println(v.getTopSpeed());
       System.out.println(v.getMaxPassengers());
   }
}

//Output


Related Solutions

Define an interface called Shape that requires methods to get x and y, and a method...
Define an interface called Shape that requires methods to get x and y, and a method to calculate the area. Call these methods getX(), getY(), and getArea(). Write a class called MyRectangle that implements the Shape interface and contains: the double data fields x, y , and width with get and set methods a no-arg constructor that creates a default rectangle with 0 for both x and y and 1 for both length and width a constructor that creates a...
3. Which statement about methods in an interface is true? A.)All methods in an interface must...
3. Which statement about methods in an interface is true? A.)All methods in an interface must be explicitly declared as private or public. B.)All methods in an interface are automatically public. C.)All methods in an interface are automatically private. D.)All methods in an interface are automatically static.
create an interface called Visible that has two methods called makeVisiible, makeInvisible. both methods take no...
create an interface called Visible that has two methods called makeVisiible, makeInvisible. both methods take no parameters and should return a boolean result. HTML EditorKeyboard Shortcuts 12pt Paragraph 0 words Flag this Question Question 58 pts The following classes have been created with the given behaviors: public class Leo extends Don { public void method1() { System.out.print("Leo 1 "); } public void method3() { System.out.print("Leo 3 "); } public String toString() { return "Leo 1 "; } } class Mike...
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
Finish the following java question: Consider the following interface: interface Duty { public String getDuty(); }...
Finish the following java question: Consider the following interface: interface Duty { public String getDuty(); } Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor....
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
1. Create a base class called Vehicle that has the manufacturer’s name (type String), number of...
1. Create a base class called Vehicle that has the manufacturer’s name (type String), number of cylinders in the engine (type int). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double) and towing capacity in tons (type double). Add the following methods to the Vehicle class: A default constructor that sets the vehicle class properties to default values (manufacturer = "None"; cylinders = 0). A constructor that...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT