2 Replies - 1753 Views - Last Post: 28 May 2012 - 09:44 AM Rate Topic: -----

#1 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Question - Interface - Assignment

Posted 28 May 2012 - 08:09 AM

Hi Java Experts,

I am required to do another assignment. I will post the question and the code. Tell me if I have got it or not. If not, what am i missing? My prof is not much of help.So seeking your advise.

Question: 1. Create an interface called Vehicle. In this interface create a method called printData()
2. Implement interface Vehicle by classes Car, SUV, Minivan
3. In each class override a method toString() and create a string which holds all data
4. In the method printData in each class print the data using toString() method.

 public interface Vehicle
	// this is a method
	void printData();

below - car class - the other two classes - van and suv are identical to this class

 public class Car implements Vehicle
    // constants are declared
    String manufacturer;
    Integer year;
    String colour;
    String size;
    /* Please enter all the string parameters within quotes. For the Size your options
     * are compact,midsize,large
    // This is the constructor - instance of the car
    public Car (String manufacturer,Integer year,String colour, String size) // declaring constants specific to constructor

        // ensuring the value of constants declared outside of constructor is same as constants
        //declared inside the constructor
        this.manufacturer = manufacturer;
        this.year = year;
        this.colour = colour;
        this.size = size;
    // This is the toString method.We are creating a string representation of the object.
    public String toString()
        String line1 = "Your car details are:"; // assigning value for line1 which has a string data type
        // we return the values using the return statement
        return line1 + "\n Manufacturer:" +manufacturer+  //  \n puts the string to next line 
        "\n Year:" +year+                                //and + links the string with declared constants
        "\n Colour:" +colour+
        "\n Size:" +size;
    // This is the printData method.We print the data using the toString method.
    public void printData()
        // The println statement takes the string information from the toString method 
        //and displays on the screen
        System.out.println(toString()); // invoking the toString method


Is This A Good Question/Topic? 0
  • +

Replies To: Question - Interface - Assignment

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1457
  • Posts: 3,282
  • Joined: 30-January 11

Re: Question - Interface - Assignment

Posted 28 May 2012 - 08:28 AM

Seems fine to me.

The only thing really is your fields (not constants like in the comment) should be private (assuming that you have been taught about that).

Plus normally you store 'ints' instead of 'Integers' so Integer year would be int year and the same in the constructor. But of course if you've been taught that way don't worry about it.

Best way to find out if you've done something correctly is to just go ahead and run it and see if it does what you expect it to do.

This post has been edited by Ryano121: 28 May 2012 - 08:29 AM

Was This Post Helpful? 1
  • +
  • -

#3 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: Question - Interface - Assignment

Posted 28 May 2012 - 09:44 AM

Thank you. You have been a great help. I have adjusted as per your advise and submitted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1