Question

Write a CarInterface to have the following method headers: public int pumpGas(int gas); // no more...

Write a CarInterface to have the following method headers:

public int pumpGas(int gas); // no more than 15 gallons return cost at $3.25 per gallon

public void goFast(int fast); // add fast to speed

public void goSlow(int slow); subtract slow from speed but if negative set speed to zero

public void stop(); //set speed to zero

Make sure to write a toString for the class Car.

Test the Car class in main by creating an array of size 10. Test all the methods in interface.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//CarInterface.java
public interface CarInterface {
    public int pumpGas(int gas); // no more than 15 gallons return cost at $3.25 per gallon

    public void goFast(int fast); // add fast to speed

    public void goSlow(int slow); //subtract slow from speed but if negative set speed to zero

    public void stop(); //set speed to zero
}

/////////////////////////////////////////////////////////////////////////////////

//Car.java
public class Car implements CarInterface{
  private int gas;
  private int speed;

  @Override
  public int pumpGas(int gas) {
    this.gas = gas;
    if(gas <= 15){
      return (int) (gas*3.25);
    }
    else{
      return -1;
    }
  }

  @Override
  public void goFast(int fast) {
    speed += fast;
  }

  @Override
  public void goSlow(int slow) {
    speed -= slow;
    if(speed<0){
      speed = 0;
    }
  }

  @Override
  public void stop() {
    speed = 0;
  }

  public String toString() {
    return "Car{" +
            "gas=" + gas +
            ", speed=" + speed +
            '}';
  }
}

/////////////////////////////////////////////////////////////////////////////////

//TestCar.java
public class TestCar {
    public static void main(String[] args) {
        CarInterface cars[] = new Car[10];

        for(int i = 0;i<cars.length;i++){
            cars[i] = new Car();
        }

        for(int i = 0;i<cars.length;i++){
            System.out.println(cars[i].pumpGas(i));
        }
        System.out.println();

        cars[0].goFast(34);
        cars[3].goFast(67);
        cars[4].goFast(23);
        cars[5].goFast(1);
        cars[8].goFast(7);

        for(int i = 0;i<cars.length;i++){
            System.out.println(cars[i]);
        }
        System.out.println();

        cars[0].goSlow(3);
        cars[3].goSlow(4);
        cars[4].goSlow(6);
        cars[5].stop();
        cars[8].goSlow(1);

        for(int i = 0;i<cars.length;i++){
            System.out.println(cars[i]);
        }
        System.out.println();

    }
}

/////////////////////////////////////////////////////////////////////////////////

Add a comment
Know the answer?
Add Answer to:
Write a CarInterface to have the following method headers: public int pumpGas(int gas); // no more...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Adapt your Rational class : public class Rational { private int num; private int denom; public...

    Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • skeleton: import java.util.*; public class Point3D{ private int x,y,z; Point3D(int a, int b, int c){ x = a; y = b; z = c; } public int x(){ return x; } public int y(){ return y; } public in...

    skeleton: import java.util.*; public class Point3D{ private int x,y,z; Point3D(int a, int b, int c){ x = a; y = b; z = c; } public int x(){ return x; } public int y(){ return y; } public int z(){ return z; } public String toString(){ return "("+x+","+y+","+z+")"; } } Question1Test.java import java.util.*; public class Question1Test { public static void main(String[] args){ MyHashList<Point3D> lst = new MyHashList<Point3D>(1000); } } a) A class Point3D is given. Re-write this class so that...

  • In Worked Example 10.1, add a default method default int [] values(int n) that yields an...

    In Worked Example 10.1, add a default method default int [] values(int n) that yields an array of the first n values of the sequence. Please use only the code provided below from the worked example and if possible provide screenshots using all the code provided and the test class provided: Sequence.java public interface Sequence { int next(); } LastDigitDistribution.java public class LastDigitDistribution { public void process(Sequence seq, int valuesToProcess) {       for (int i = 1; i <=...

  • Exercise 2: Write array methods that carry out the following tasks for an array of integers...

    Exercise 2: Write array methods that carry out the following tasks for an array of integers by creating and completing the “ArrayMethods” class below. Add documentation comments for each method. Provide a test program called 'Lab5_yourID.java” that test methods of ArrayMethods class. In your test program, use random class to generate array values. public class ArrayMethods { private int[ ] values; //declare instant variables public ArrayMethods (int[] initialValues) {values = initialValues;} //constructor public void shiftRight() { } public Boolean adjacentDuplicate()...

  • q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have...

    q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT