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: instructions on assignment

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default instructions on assignment

    Ok This is going to seem like a wierd question but I am working on a program and need some clarification. I tried to contacting my instructor but he has not gotten back to me yet. My issue is I am not sure how my instructor wants me to figure out how to start, drive , and stop the car in my program. I will give you the instructions below and my code(I know the start, drive, and stop functions are not correct but I was just playing with this program to get started with it)

    Create a Java application consisting of two classes. Class one will be your application class. Class two will be a class called Car. Your application will create an instance of Car, called nova, and drive it.

    Rules for the car:
    You can’t drive a car if it is not started (send an error message to the console).
    You can’t stop a car if it is not started (send an error message to the console).
    You can’t start a car if it is already started (send an error message to the console).

    Once you tell the car to drive, the only thing you can do is stop (Send a message to the console)

    Once you call stop, the car will return to the initial state and the user must start the car before attempting to do any other functions. (Send a message to the console)

    The purpose of the showState method is provide a way to inspect the state of the car. It should build a message, which can then be sent to the console.

    Scenario 1:

    1) Create instance of car
    2) Start the car
    3) Send status message to the console
    4) Drive the car
    5) Stop the car
    6) Send status message to the console

    Scenario 2:

    1) Create instance of car
    2) Send status message to the console
    3) Drive the car
    4) Stop the
    5) Send status message to the console

    Scenario 3:
    1) Create instance of car
    2) Send status message to the console
    3) Start the car
    4) Send status message to the console
    5) Stop the car
    6) Send status message to the console
    7) Drive the car
    8) Stop the car


    here id my code
    public class ProgrammingAssignment3
    {
    	public static void main(String [] args)
    	{
    		System.out.println("Start the app");
     
    		Car myCar = new Car();
     
    		//Function to start the car
    		myCar.start();
     
    		//Function to drive the car
    		myCar.drive();
     
    		//Function to stop the car
    		myCar.stop();
     
     
    		//System.out.println(myLight.watts);
    		//System.out.println(myLight.getIsOn());
     
    		//myLight.on();
    		//System.out.println(myLight.getIsOn());
     
    		System.out.println("End of app");
    	}
    }
     
    //class lightBulb
    class Car
    {
    	//public int watts = 100;
    	private boolean isStarted = true;
     
    	public void start()
    	{
    		isStarted = (true);
    		System.out.println("Start the car.");
    	}
    	public void drive()
    	{
    		while(isStarted = (false))
    		{
    			System.out.println("Error!");
    		}
    		System.out.println("Drive the car.");
     
    	}
    	public void stop()
    	{
    		while(isStarted = (false))
    		{
    			System.out.println("Error!");
    		}
    		System.out.println("Stop the car.");
    	}
    	//public String showState()
    	//{
     
    		//return 0;
    	//}
     
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: instructions on assignment

    Quote Originally Posted by bkruep View Post
    Ok This is going to seem like a wierd question ...
    I guess you forgot to include it.
    What is your question?

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: instructions on assignment

    what is the problem with this code?

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: instructions on assignment

    the code was wrking fine

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: instructions on assignment

    Sorry what I am trying to ask is kind of confusing so I will try as best I can. My issue is more or less in the instructions I was given. For example, I was thinking about putting a statement like System.out.print("What would you like the car to do? Press 1 to start the car, 2 to drive, or 3 to stop");

    However it does not tell me to do that in the instruction so i am not sure how I am supposed to have any error messages come up or how my output comes up the way it does. I am not sure if that explains it any better to whhat I am trying to do and if that doesn;t help Ill try to explain it differently

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: instructions on assignment

    Quote Originally Posted by bkruep View Post
    Sorry what I am trying to ask is kind of confusing so I will try as best I can. My issue is more or less in the instructions I was given. For example, I was thinking about putting a statement like System.out.print("What would you like the car to do? Press 1 to start the car, 2 to drive, or 3 to stop");

    However it does not tell me to do that in the instruction so i am not sure how I am supposed to have any error messages come up or how my output comes up the way it does. I am not sure if that explains it any better to whhat I am trying to do and if that doesn;t help Ill try to explain it differently
    Look into using switch statements, I believe that would allow you to do what yo wish, with switch statements you can have the user input a int (in your case 1 to start 2 to drive 3 to stop) and depending on what they do you "switch" to that set of conditions, and you could also use the switch "default" where you could put an error message if they give you an entry that isn't one of your switch cases.

    EDIT: Or even a while loop or else/if statements could work, although they would be more complicated to integrate then a switch statement, check here for a simple switch tutorial that should give you a good idea on how they work:

    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  2. Assignment please Help :(
    By pagalas07 in forum Collections and Generics
    Replies: 3
    Last Post: March 30th, 2011, 08:52 AM
  3. Looking for guidance and instructions
    By coyboss in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2011, 10:57 AM
  4. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  5. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 09:44 PM