Results 1 to 7 of 7
  1. #1
    Angelar is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default Problem with case - might need recursion

    Hey this is my 1st thread

    I have this class

    Java Code:
    class Vehicle {
        private int Aukswn; //Number of cars entered the parking area
        private String Pinakida; //The cars license plate number
        private String Type; // Car type
        private int Position; //The position the car is going to park
        
    
        public Vehicle() {
           Aukswn = 0;
           Pinakida = new String();
           Type = new String();
           Position = 0;
        }
    
        public void Vehicle (int Aukswn, String Pinakida, String Type, int Position) {
            this.Aukswn = Aukswn;
            this.Pinakida = Pinakida;
            this.Type = Type;
            this.Position = Position;
        }
    
        public int getAukswn() {
            return this.Aukswn;
        }
        public String getPinakida() {
            return this.Pinakida;
        }
        public String getType() {
            return this.Type;
        }
        public int getPosition() {
            return this.Position;
        }
    }
    Java Code:
    public class Main {
                 public static void main (String args[]) {
                 int i, choice;
            Vehicle array[] = new Vehicle[10];
            do{
                System.out.print("Main menu");
                System.out.print("1. Arrival of Vehicle");
                System.out.print("2. Vehicle Properties");
                System.out.print("3. Free parking slots");
                System.out.print("4. End");
                System.out.print("Choice (1-4) :");
                choice = UserInput.getInteger();
                    switch (choice) {
                        case (1):
                                    
                                    }
                                
                          
            }
            while (choice == 4);
        }
    }
    Now I find it a bit complicated to explain in English but I will try

    For the 1st case, I will need to
    Send a message to the user that there are any free parking slots (variable position = 0),and if there are, display the index of the first free slot to the user (else it will send him back to the main menu)

    If there is a free parking slot, the user should enter the properties of the vehicle (I have my Insert class for that purpose, but for some reason I cant use it under a case)

    Java Code:
    public void Insert() {
    System.out.print ("Give the number of cars entered the parking area");
    this.Aukswn = UserInput.getInteger();
    System.out.print ("Give the car's license plate number");
    this.Pinakida = UserInput.getString();
    System.out.print ("Give the parking slot which the car entered");
    this.Position = UserInput.getInteger();
    System.out.print ("Give the type of the car");
    this.Type = UserInput.getString();
    }
    The position the user enters should be the one the program displayed at him
    If he does not enter this position, or an invalid number (e.g. > 10) it should return him to the main menu
    After the user succesfully enters the mentioned properties of the vehicle, the program returns him to the main menu. It should also return him if he presses the Esc. button during entering them
    Any opinions?

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Hello Angelar and welcome.

    I presume this is assignment work so I will only point you in the right direction for now.
    I have made a few assumptions about you code so correct me if I missunderstand. Even so, there are a several issues I can spot straight off.

    Firstly, the variable "Aukswn" refers to the number of cars in the parking lot. If so, why is this variable in the Vehicle class? If you have to store it, then I would suggest placing it somewhere else, perhaps even create a CarPark class.

    Secondly, you do while loop does the opposite of what you want. It will only loop if the user wants to exit the program.

    Thirdly, I don't think you meant to refer to Insert as being a class, but if you did the method Insert does not contain any reference to the Vehicle class or so neither construct nor effect the properties of an instance of Vehicle.
    If on the other hand you meant to refer to Insert as a method within the Vehicle class, then I am guessing that an instance of Vehcle was not created prior to the use of the method or you are not calling it correctly.

    Once this lot is sorted, then I would like to hear you thoughts on how to solve the problems you have asked about prior to giving my own ;).

    Regards.

  3. #3
    Angelar is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Ronin View Post
    Hello Angelar and welcome.

    I presume this is assignment work so I will only point you in the right direction for now.
    I have made a few assumptions about you code so correct me if I missunderstand. Even so, there are a several issues I can spot straight off.

    Firstly, the variable "Aukswn" refers to the number of cars in the parking lot. If so, why is this variable in the Vehicle class? If you have to store it, then I would suggest placing it somewhere else, perhaps even create a CarPark class.

    Secondly, you do while loop does the opposite of what you want. It will only loop if the user wants to exit the program.

    Thirdly, I don't think you meant to refer to Insert as being a class, but if you did the method Insert does not contain any reference to the Vehicle class or so neither construct nor effect the properties of an instance of Vehicle.
    If on the other hand you meant to refer to Insert as a method within the Vehicle class, then I am guessing that an instance of Vehcle was not created prior to the use of the method or you are not calling it correctly.

    Once this lot is sorted, then I would like to hear you thoughts on how to solve the problems you have asked about prior to giving my own ;).

    Regards.
    About the variable Aukswn, it is given from the assignment along with the other attributes of the Vehicle class, thats why I put it there. It did not seem too reasonable to me as well.
    About the insert method, I guess I can do it without using a method, just applying UserInput straight away inside my case.
    About the loop, I sorted that out

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    You could declare the variable Aukswn as static, in which case it will be shared between all of the instances. This is something I generally try to avoid.

    I would still like to hear your thoughts on how to solve your other problems before assisting further.

    Regards.

  5. #5
    Angelar is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    public class Main {
            public static void main (String args[]) {
                 int i;
                 int choice = 0;
            	Vehicle array[] = new Vehicle[10];
            	while (choice <> 4) {
                        System.out.print("Αρχικό Μενού");
                        System.out.print("1. Προσέλευση Οχήματος");
                        System.out.print("2. Εμφάνιση Στοιχείων Οχήματος");
                        System.out.print("3. Λίστα Θέσεων Parking");
                        System.out.print("4. Τέλος");
                        System.out.print("Δώστε Επιλογή (1-4) :");
                        choice = UserInput.getInteger();
                    	switch (choice) { 
                        	case (1):
                            if (array.Position[i] = 0) {
                                System.out.print(" Η πρωτη κενη θεση ειναι η: " + i);
                                    System.out.print("Δωσε αριθμο πινακιδων");
                                    array.Pinakida = UserInput.getString();
                                    System.out.print("Δωσε θεση παρκινγκ (Χρησιμοποιησε την πρωτη ελευθερη)");
                                    array.Position = UserInput.getInteger();
                                    System.out.print ("Δωσε τυπο οχηματος");
                                    array.Type = UserInput.getString();
    			    
                            else return;
    			}
    		}
    	}
    }
    }
    This is what I used now. I left Aukswn out of this (will figure out about it later on). I used a while loop, it seems to be working better with it and used the UserInput without creating the Insert method. Now what I can't say for sure is how to make the program recognize if the entered command is correct or if you pressed the Esc. button so it will return you to the main menu. Opinions so far?

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Here's a few things I can suggest:
    1) Make sure your code compiles before posting it. Your {}s don't line up, and this prevents us from being able to compile it and run it without figuring out which {}s should be removed.
    2) Check your = vs ==, particularly just after your first "case" statement.
    3) <> is not a valid Java operator (this is obsolete and only present as legacy code in a few languages, e.g. Python). Use != instead.
    4) You declare the variable i, and use it later, but its value is never set.
    5) UserInput is never declared.

    Sorry if this seems a big harsh to just point them all out like that, but they're all issues that need addressing.

    Best of luck. :)

  7. #7
    Angelar is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Here's a few things I can suggest:
    1) Make sure your code compiles before posting it. Your {}s don't line up, and this prevents us from being able to compile it and run it without figuring out which {}s should be removed.
    2) Check your = vs ==, particularly just after your first "case" statement.
    3) <> is not a valid Java operator (this is obsolete and only present as legacy code in a few languages, e.g. Python). Use != instead.
    4) You declare the variable i, and use it later, but its value is never set.
    5) UserInput is never declared.

    Sorry if this seems a big harsh to just point them all out like that, but they're all issues that need addressing.

    Best of luck. :)
    About the {}s, NetBeans pops out many mistakes, I try to write the code in notepad trying to line them up, but they pop up again when i paste it in NetBeans
    2 and 3 are solved
    UserInput is declared in a separate file
    And i should be 0

    Java Code:
    public class Main {
      public static void main (String args[]) {
        int i = 0;
        int choice = 0;
        Vehicle array[] = new Vehicle[10];
        while (choice != 4) {
        	System.out.print("Αρχικό Μενού");
            System.out.print("1. Προσέλευση Οχήματος");
            System.out.print("2. Εμφάνιση Στοιχείων Οχήματος");
            System.out.print("3. Λίστα Θέσεων Parking");
            System.out.print("4. Τέλος");
            System.out.print("Δώστε Επιλογή (1-4) :");
            choice = UserInput.getInteger();
            	switch (choice) { 
                    case (1):
                    	if (array.Position[i] == 0) {
                            System.out.print(" Η πρωτη κενη θεση ειναι η: " + i);
                            System.out.print("Δωσε αριθμο πινακιδων");
                            array.Pinakida = UserInput.getString();
                            System.out.print("Δωσε θεση παρκινγκ (Χρησιμοποιησε την πρωτη ελευθερη)");
                            array.Position = UserInput.getInteger();
                            System.out.print ("Δωσε τυπο οχηματος");
                            array.Type = UserInput.getString();
    			    
                            else return;
    			}
    		}
        }
      }
    }
    Last edited by Angelar; 10-13-2010 at 05:36 PM. Reason: added code

Similar Threads

  1. Recursion problem
    By luke in forum New To Java
    Replies: 6
    Last Post: 10-06-2010, 06:35 AM
  2. recursion and call stack problem
    By OptimusPrime in forum New To Java
    Replies: 4
    Last Post: 12-26-2009, 09:49 PM
  3. Java Recursion Problem
    By gmnnn in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-06-2009, 04:22 PM
  4. MySQL Case Sensitive Problem
    By techissue2008 in forum JDBC
    Replies: 1
    Last Post: 06-10-2008, 06:23 AM
  5. problem with operator in case
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 03-21-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •