Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: have code - need one line fixed please!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default have code - need one line fixed please!

    hihi! here's my prompt and i think i did the program right but I can't get it to work

    how do i fix this program?

    this is the prompt btw: Create a S tudent class that stores the name, address, GPA, age, and major for a student. Additionally, the
    S tudent class should implement the C omparable interface. Make the comparison of S tudent objects
    based on the name, then the address, then the major, and finally the GPA. If the difference between two
    objects when compared lies in the name, return a 1 or -1 depending on which object is lessor/greater.
    If the difference in objects is in the address, return +/- 2. If the difference is in the major, return +/- 3. If the
    difference is in the GPA, return +/- 4. Only return zero if all 4 fields are the same. This behavior deviates
    slightly from the normal c ompareTo behavior (<0, 0, or >0), but we are ok with that for the purposes of
    this lab.
    Write a class called S tudentDriver to test your new method. Hard code several students into the main
    method of the S tudentDriver class with small differences in different fields. That is, make one student
    have one name and another a second name. Make a third student have the same name as the first, but a have
    the third student have a different address, etc. Make sure your c ompareTo method finds each the
    differences correctly.

    the first is my comparable and the second is my driver
    thanks!

    public class Student implements Comparable {
    String name;
    String address;
    String major;
    double gpa;
     
    public int compareTo(Object st) {
    	Student s = (Student) st;
    	if(this.name.compareTo(s.name) == 0) {
    		if(this.address.compareTo(s.address) == 0) {
    			if(this.major.compareTo(s.major) ==0) {
    				if(this.gpa == s.gpa) {
    					return 0; }
    				else if(this.gpa < s.gpa) {
    					return -4; }
    				else {
    					return 4; }}
    			else {
    				return 3 * this.major.compareTo(s.major); }}
    		else {
    			return 2 * this.address.compareTo(s.address); }}
    	else {
    		return this.name.compareTo(s.name);}
    }}

     
    public class StudentDriver{
     
        public static void main(String[] args) {
          Student st = new Student();
          Student Bob = new Student("Bob", "3 Bella Vista Court", "Phys", .8);
          Student Bob = new Student("Bob", "4 Bella Vista Court", "Chem", .9);
          Student Bob = new Student("Janice", "8 Bella Vista Court", "Eng", .7);
          Student Bob = new Student("Ian", "5 Bella Vista Court", "Math", .2);            
          }
     
    }
    Last edited by ash12; August 7th, 2012 at 08:20 PM.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: have code - need one line fixed please!

    Student implements an interface with name Comparable.So it has to implement,thus write body of every single function of the interface.If one or more functions of the interface is left without an implementation,then the class that implements the interface has to be abstract.What i am saying in code is like this
    package airplane;
     
    public interface AirplaneInterface {
        void print(String runway);
        void setEngines(boolean on);
        void setGears(boolean down);
    }
    and i have another file
    package airplane;
     
    public class AirplaneSuperclass implements AirplaneInterface {
        //fields
        private boolean[] enginesIdle;
        private short enginesNumber;
        private int altitude;
        private String state;//taxiing,landing etc.
        private String flightNo;
        private boolean gearDown;
        private String type;
     
     
        //methods
        void print(String runway)
        {
              //body
        }
     
        void setEngines(boolean on)
        {
             //body
         }   
     
         void setGears(boolean down)
        {
              //body
        }
    everything ok.
    Now if function setEngines was not implemented(had a body) then i would get the error
     java.lang.RuntimeException: Uncompilable source code - airplane.AirplaneSuperclass is not abstract and does not override abstract method setEngines (boolean on) in airplane.AirplaneInterface
    	at airplane.AirplaneSuperclass.<clinit>(AirplaneSuperclass.java:4)

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. Explanation of second argument in the following line of code
    By brighamandrew in forum Java Theory & Questions
    Replies: 1
    Last Post: September 11th, 2011, 09:39 AM
  3. enum, value of: Why is this code line structured the way it is?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 5
    Last Post: March 28th, 2011, 09:15 AM
  4. Help with a line of my code.
    By Miical94 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 7th, 2011, 11:00 AM
  5. Missing a line or two of code,b ut dont know what it is.
    By backdown in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 20th, 2011, 09:13 AM