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 6 of 6

Thread: bluej some questions about a simple car details class

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

    Default bluej some questions about a simple car details class

    Hallo ,
    this is a class in bluej about car details i created
    the problems so far in the class are that it allows any number in the transmition when i go to create the class , it should only allow 1 and 2(i dont really need to do it , but i am asking if there is a way around it ) and also in the toString method to show the manual and automatic and not 1 and 2 in the type

    thanks for all the help
    public class Car 
    {
     
      // set up the two constants for the transmition 
      public static final int manual = 1;
      public static final int automatic = 2;
      // instance variables - replace the example below with your own
        private static  int  engineSize;
        private int  fuel;
        private final int type ;
     
         /**
         * Constructor for objects of class Car
         */
        public Car(int newFuel , int newEngineSize , int Trans  ) {
           fuel = newFuel;
           engineSize = newEngineSize;
           type = Trans;
            }
         /**
         Change the fuel 
         */
     
        public void getFuel()
        {
         System.out.println("The fuel of this car is " + fuel  );
        }
     
        public void getEngineSize()
        {
        System.out.println("The engineSize of this car is " +  engineSize);    
        }
     
        public  void setFuel( int newfuel)
        {
        fuel = newfuel;
        }
     
        public String getTypeAsString (){
    		if (type == automatic){
    			return "automatic";
    		}
    		else if (type == manual){
    			return "manual";
    		}
    		return "";
    	}
     
     
        public String toString(){
          return ("fuel "  + fuel + " Engine Size " + engineSize + " Transmition  " + type ) ;
     
        }
     }
    Last edited by infernocy; April 7th, 2012 at 01:13 PM. Reason: some writting errors


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: bluej some questions about a simple car details class

    You can validate the data when it is received in the constructor and throw an exception if the data is invalid. Many java classes do that. See the Integer class for an example.
    You would need to add a throws clause to the constructor's definition.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    infernocy (April 7th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: bluej some questions about a simple car details class

    thanks i will try
    where can i find the integer class ?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: bluej some questions about a simple car details class

    The doc for all the java SE classes:
    Java Platform SE 6
    Find the class in the lower left, click it and the doc will show in the main frame.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bluej some questions about a simple car details class

    excuse me im the beginner, i want to ask how entering source code like that, thank you

  7. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: bluej some questions about a simple car details class

    Quote Originally Posted by erdy_rezki View Post
    excuse me im the beginner, i want to ask how entering source code like that, thank you
    When you post select the Go Advanced button, copy and paste your code and use the highlight tags, or select it and then click the "#" button above the text area. Here is the list of all the tags and how to use them.

Similar Threads

  1. Questions on simple client-server app
    By worwhite in forum Threads
    Replies: 24
    Last Post: July 30th, 2011, 08:19 PM
  2. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  3. Paying for simple questions.
    By kmjt in forum Paid Java Projects
    Replies: 6
    Last Post: February 10th, 2011, 08:31 PM
  4. Newbie questions about lesson on class implementation
    By Jonnybravo9 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 09:04 PM

Tags for this Thread