1 2 Previous Next 16 Replies Latest reply: Mar 10, 2008 6:35 AM by YoungWinston RSS

    Multiple Class problems- interfaces, setters, getters, and variable issues

    807601
      I have an abstract interface which i know is right. here it is
      public interface Cost
      {
           public double getCost();
           public void setCost (double cost);
           public double computeCost();
      }     
                
      the automobile class implements it, and i've got a few errors. if anyone could help me that would be great
      import java.text.DecimalFormat;
      
      public class Automobile implements Cost
      {
           private String make;
           private int year;
           private double miles, autoCost;
           
           public Automobile ()
           {
                make = "NA"; 
                year = 2000;
                miles = 0.0;
                autoCost = 0.0;
           }     
           
           public Automobile (String make, int year, double miles)
           {
                this.make = make;
                this.year = year;
                this.miles = miles;
                autoCost = 0.0;
           }
           
           public String getMake()
           {
                return make;
           }
           
           public int getYear()
           {
                return year;
           }
           
           public double getMiles()
           {
                return miles;
           }
           
           public void setMiles (double miles)
           {
           this.miles = miles;
           }
           
           public double getCost()
           {
                return autoCost;
           }
           
           public void setCost (double cost);
           {
                return cost;
           }
           
           public double computeCost()
           {
                double cost = 30000;
                
                if (make.equalsIgnoreCase("A"))
                     cost = cost - 0;
                else
                     if (make.equalsIgnoreCase("B"))
                          cost = cost - 5000;
                     else 
                          if (make.equalsIgnoreCase("C"))
                               cost = cost - 10000;
                          else
                               cost = cost - 15000;
                
                if (year < 2008)
                     cost = cost - ((2008 - year)*500);               
                          
                if (miles > 0)
                     cost = cost - (miles*.1);
                else 
                     cost = cost - 0;
                
                if (cost > 0)
                     return cost;
                else
                {     
                     cost = 0;
                     return cost;
                }                         
           }
           
           public String toString()
           {
                String result; 
                
                DecimalFormat fmt = new DecimalFormat ("$###,###,##0.##");
                
                result = "Make: " + make +"\n" + "Year: " + year + "\n"; 
                result +=  "Miles: " + miles + "\n" + "Cost: ";
                result +=  fmt.format(cost);
                     
                return result;
           }
      }          
        • 2. Re: Multiple Class problems- interfaces, setters, getters, and variable issues
          807601
          And what are the errors??????? At least on the first glance there is nothing wrong with your code at least concerning the implementation of cost.
          • 3. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
            807601
            error noted in comments:
                public void setCost (double cost); // *** check that semicolon
                {
                    return cost; // *** yikes! is this for setCost or getCost???
                }
                public String toString()
                {
                    String result; 
                    
                    //** better to use NumberFormat.getCurrencyInstance() here
                    DecimalFormat fmt = new DecimalFormat ("$###,###,##0.##");
                    result = "Make: " + make +"\n" + "Year: " + year + "\n"; 
                    result +=  "Miles: " + miles + "\n" + "Cost: ";
                    result +=  fmt.format(cost); //**** does your class have a cost variable??
                        
                    return result;
                }
            Edited by: Encephalopathic on Mar 10, 2008 1:54 AM
            • 4. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
              807601
              It would help if you told us what the compile errors actually were instead of just saying "a few errors". Are we supposed to be mind readers?

              Anyway I expect most of your problems are caused by the semicolon on the end of this line in your Automobile class:

              public void setCost (double cost);

              Take the semicolon off and see how you go
              • 5. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                807601
                Automobile.java:50: missing method body, or declare abstract
                     public void setCost (double cost);
                                    ^
                Automobile.java:52: return outside method
                          return cost;
                                ^
                Automobile.java:95: cannot find symbol
                symbol  : variable cost
                location: class Automobile
                          result +=  fmt.format(cost);
                                                      ^
                3 errors
                • 6. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                  807601
                  so apparently the biggest problems are stupid and right in front of you nose.. but what about that last error
                  • 7. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                    807601
                    jrb0006 wrote:
                    but what about that last error
                    Look at the line of code carefully.
                    • 8. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                      807601
                      As already mentioned one problem is the semicolon after the method declaration. The second error is pretty self-explanatory. The compiler cannot find the variable cost, because it does simply not exist in your toString method. Plus in your setCost method you return the double again, which is pretty odd...
                      • 9. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                        807601
                        Well you have an instance variable called autoCost, but not one called cost. Where is that variable supposed to come from that you're using in your toString() method?
                        • 10. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                          807601
                          Ok, I've got this so far; sorry for the stupid mistakes. Let me write the driver program, and I'm sure I'll need to post again.


                          import java.text.NumberFormat;
                          
                          public class Automobile implements Cost
                          {
                               private String make;
                               private int year;
                               private double miles, autoCost, cost;
                               
                               public Automobile ()
                               {
                                    make = "NA"; 
                                    year = 2000;
                                    miles = 0.0;
                                    autoCost = 0.0;
                               }     
                               
                               public Automobile (String make, int year, double miles)
                               {
                                    this.make = make;
                                    this.year = year;
                                    this.miles = miles;
                                    autoCost = 0.0;
                               }
                               
                               public String getMake()
                               {
                                    return make;
                               }
                               
                               public int getYear()
                               {
                                    return year;
                               }
                               
                               public double getMiles()
                               {
                                    return miles;
                               }
                               
                               public void setMiles (double miles)
                               {
                               this.miles = miles;
                               }
                               
                               public double getCost()
                               {
                                    return autoCost;
                               }
                               
                               public void setCost (double cost)
                               {
                                    
                               }
                               
                               public double computeCost()
                               {
                                    double cost = 30000;
                                    
                                    if (make.equalsIgnoreCase("A"))
                                         cost = cost - 0;
                                    else
                                         if (make.equalsIgnoreCase("B"))
                                              cost = cost - 5000;
                                         else 
                                              if (make.equalsIgnoreCase("C"))
                                                   cost = cost - 10000;
                                              else
                                                   cost = cost - 15000;
                                    
                                    if (year < 2008)
                                         cost = cost - ((2008 - year)*500);               
                                              
                                    if (miles > 0)
                                         cost = cost - (miles*.1);
                                    else 
                                         cost = cost - 0;
                                    
                                    if (cost > 0)
                                         return cost;
                                    else
                                    {     
                                         cost = 0;
                                         return cost;
                                    }                         
                               }
                               
                               public String toString()
                               {
                                    String result; 
                                    
                                    NumberFormat fmt = NumberFormat.getCurrencyInstance();
                                              
                                    result = "Make: " + make +"\n" + "Year: " + year + "\n"; 
                                    result +=  "Miles: " + miles + "\n" + "Cost: ";
                                    result +=  fmt.format(cost);
                                         
                                    return result;
                               }
                          }          
                          • 11. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                            807601
                            Your main problem is that you are going about programming all wrong. You need to add code incrementally and safely. You do this by careful iteration:

                            * Create a class skeleton. Make sure it compiles and runs. If OK, then...
                            * Add a small amount of code. Make sure it compiles and runs. If OK, then...
                            * Add another small amount of code. Make sure it compiles and runs. If OK, then...
                            * Add another small amount of code. Make sure it compiles and runs. If OK, then...
                            //.... continue iteration until done.

                            This way you don't end up with a gazillion errors to deal with.
                            Let me write the driver program, and I'm sure I'll need to post again.
                            And no, you won't be doing yourself any favors by coming back in a short time with a bunch of similar errors. These are errors that you should be able to fix if you carefully read what the compiler is telling you is wrong. Debugging is a skill that will only get better with practice. So please practice.
                            • 12. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                              800308
                              Your main problem is that you are going about programming all wrong. You need to
                              add code incrementally and safely. You do this by careful iteration:
                              Pete, can you have a chat to my boss please... Or can I just come and work for you ;-)

                              He insists that I write the whole system in Microsoft Frickin' Word, so that he can review it, before I get anywhere near an IDE or a compiler... I'm exagerating, but it still feels that way some days. Today just happens to be one of those days.

                              A marathon is sucession of small steps, and they don't usually put landmines in a marathon track, or canyons, craters, or dress up flying pigs as user reps.

                              ;-) Keith.
                              • 13. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                                807601
                                alright; although I'm sure everyone will completely crush me because that's what beginners apparently deserve...
                                I need help at the very least understanding the process by which this takes place.
                                1) Create two sets of local variables of type String, int and double corresponding to make, year, and miles
                                2) Next, prompt the user for input to set the aforementioned variables
                                3) Then, create two Automobile objects using the variables above as the parameters for the constructor
                                4) Also create a third automobile object using the constructor with no parameters
                                5) Compute cost of each Automobile and set thee autoCost of each object to the computed amount
                                6) Finally print out the info of all 3 automobiles

                                abstract interface:
                                public interface Cost
                                {
                                     public double getCost();
                                     public void setCost (double cost);
                                     public double computeCost();
                                }     
                                          
                                Automobile class:
                                import java.text.NumberFormat;
                                
                                public class Automobile implements Cost
                                {
                                     private String make;
                                     private int year;
                                     private double miles, autoCost, cost;
                                     
                                     public Automobile ()
                                     {
                                          make = "NA"; 
                                          year = 2000;
                                          miles = 0.0;
                                          autoCost = 0.0;
                                     }     
                                     
                                     public Automobile (String make, int year, double miles)
                                     {
                                          this.make = make;
                                          this.year = year;
                                          this.miles = miles;
                                          autoCost = 0.0;
                                     }
                                     
                                     public String getMake()
                                     {
                                          return make;
                                     }
                                     
                                     public int getYear()
                                     {
                                          return year;
                                     }
                                     
                                     public double getMiles()
                                     {
                                          return miles;
                                     }
                                     
                                     public void setMiles (double miles)
                                     {
                                     this.miles = miles;
                                     }
                                     
                                     public double getCost()
                                     {
                                          return autoCost;
                                     }
                                     
                                     public void setCost (double cost)
                                     {
                                          
                                     }
                                     
                                     public double computeCost()
                                     {
                                          double cost = 30000;
                                          
                                          if (make.equalsIgnoreCase("A"))
                                               cost = cost - 0;
                                          else
                                               if (make.equalsIgnoreCase("B"))
                                                    cost = cost - 5000;
                                               else 
                                                    if (make.equalsIgnoreCase("C"))
                                                         cost = cost - 10000;
                                                    else
                                                         cost = cost - 15000;
                                          
                                          if (year < 2008)
                                               cost = cost - ((2008 - year)*500);               
                                                    
                                          if (miles > 0)
                                               cost = cost - (miles*.1);
                                          else 
                                               cost = cost - 0;
                                          
                                          if (cost > 0)
                                               return cost;
                                          else
                                          {     
                                               cost = 0;
                                               return cost;
                                          }                         
                                     }
                                     
                                     public String toString()
                                     {
                                          String result; 
                                          
                                          NumberFormat fmt = NumberFormat.getCurrencyInstance();
                                                    
                                          result = "Make: " + make +"\n" + "Year: " + year + "\n"; 
                                          result +=  "Miles: " + miles + "\n" + "Cost: ";
                                          result +=  fmt.format(cost);
                                               
                                          return result;
                                     }
                                }          
                                and what i have so far of my driver:
                                import java.util.Scanner;
                                
                                public class Project6A_Driver
                                {
                                     public static void main (String[] args)
                                     {
                                     String make1, make2;
                                     double year1, year2;
                                     double miles1, miles2; 
                                     Automobile auto1, auto2, auto3;
                                     
                                     Scanner scan = new Scanner (System.in);
                                
                                     System.out.print ("Enter the make of automobile #1: ");
                                     make1 = scan.nextLine();
                                     
                                     System.out.print ("Enter the year of automobile #1: ");
                                     year1 = scan.nextDouble();
                                     
                                     System.out.print ("Enter the number of miles of automobile #1: ");
                                     miles1 = scan.nextDouble();
                                
                                     System.out.println();
                                     
                                     System.out.print ("Enter the make of automobile #2: ");
                                     make2 = scan.nextLine();
                                     
                                     System.out.print ("Enter the year of automobile #2: ");
                                     year2 = scan.nextDouble();
                                     
                                     System.out.print ("Enter the number of miles of automobile #2: ");
                                     miles2 = scan.nextDouble();
                                     
                                     
                                     auto1 = new Automobile(make1, year1, miles2);
                                     auto2 = new Automobile(make2, year2, miles2);
                                     auto3 = new Automobile ();
                                     
                                     auto1.computeCost
                                     
                                     }
                                
                                }
                                I'M NOT ASKING FOR A HANDOUT, SO PLEASE DON'T TELL ME TO DO MY OWN WORK OR SOMETHING LIKE THAT. I've been working on this for a long time, and just can't figure out what direction to go on the driver program. I'm not as grounded in all of this stuff as I should be, so some things might not seem obvious to me like they are to you. I appreciate your help in advance.
                                • 14. Re: Multiple Class problems- interfaces, setters, getters, and variable iss
                                  807601
                                  please take out the auto1.computecost it wasn't intended to go in
                                  1 2 Previous Next