Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36
  1. #21
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    hardwired,

    I really appreciate your help, im the kind of guy who looks at finished code and can learn from seeing how it communicates, and I try and lookup or ask about what I do not understand, the thing is, for this project we have not gone over arrays and if I start using an array ill be left hanging if i run into a problem.

    is there a way you could help me start out a layout of how the code will look using only constructors (no arrays)?

    Thanks a lot

  2. #22
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    If I told you I was using the String Tokenizer for this project, that would make it possible to display everything without an array, correct?

  3. #23
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.JOptionPane;
    
    public class PC2 {
        public static void main(String[] args){
            // Let the DataAcquisition2 class collect
            // and validate the user input.
            DataAcquisition2 dataAcqn = new DataAcquisition2();
            dataAcqn.collectData();
            // Retrieve the collected/validated data.
            String firstName = dataAcqn.getFirstName();
            String lastName = dataAcqn.getLastName();
            String phone = dataAcqn.getPhone();
            int rolls = dataAcqn.getRolls();
            int prints = dataAcqn.getPrints();
            // Use this data to create a new instance of the
            // ProjectDefinition2 class and save a reference
            // to the instance in a local variable for later use.
            ProjectDefinition2 person =
                new ProjectDefinition2(firstName, lastName, phone, rolls, prints);
            System.out.println("person = " + person);
        }
    }
    
    class DataAcquisition2 {
        String firstName;
        String lastName;
        String phoneNumber;
        int rollsOfFilm;     // validation requirements
        int numberOfPrints;  // indicate we need [i]int[/i]s here
    
        public void collectData() {
            firstName = getItem("First Name");
            lastName = getItem("Last Name");
            phoneNumber = getItem("Phone Number");
            boolean okay = true;
            rollsOfFilm = getNumber("Rolls of Film");
            numberOfPrints = getNumber("Number of Prints");
            validateData();
        }
    
        public String getFirstName() { return firstName; }
    
        public String getLastName() { return lastName; }
    
        public String getPhone() { return phoneNumber; }
    
        public int getRolls() { return rollsOfFilm; }
    
        public int getPrints() { return numberOfPrints; }
    
        private String getItem(String s) {
            return JOptionPane.showInputDialog(null, "Enter " + s);
        }
    
        private int getNumber(String subject) {
            int n = 0;
            boolean success = false;
            do {
                // Make sure you can get a parsable value for the [i]int[/i]
                // data variables. Otherwise you will have to parse them in
                // the validateData method and you would be unable to get
                // corrected input from the user if you came up short.
                try {
                    String input = getItem(subject);
                    n = Integer.parseInt(input);
                    success = true;
                } catch(NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "Number format error: " +
                                                   e.getMessage());
                }
            } while(!success);
            return n;
        }
    
        /**
         * a. The name of the customer is not blank 
         * b. The phone number of the customer is not blank and
         *    doesn’t exceed 10 characters and is an integer
         *    with positive number only
         * c. The number of rolls is between 1 and 100 and is an
         *    positive integer 
         * d. The number of prints can only be (1,2,8,10, 25,30,50),
         *    positive integer 
         */
        private void validateData() {
            try {
                if(firstName.length() == 0 || lastName.length() == 0)
                    throw new IllegalArgumentException("names must not be blank");
                // validate the rest of the four things listed above...
            } catch(IllegalArgumentException e) {
                System.out.println("Illegal Argument: " + e.getMessage());
                System.exit(1);
            }
        }
    }
    
    class ProjectDefinition2 {
        String userNameFirst;
        String userNameLast;
        String userPhone;
        int userFilm;
        int userPrints;
        final int USER_EXPOSURES = 1;
    
        public ProjectDefinition2(String first, String last, String phone,
                                  int rolls, int prints) {
            userNameFirst = first;
            userNameLast  = last;
            userPhone     = phone;
            userFilm      = rolls;
            userPrints    = prints;
        }
    
        public String toString() {
            return "First Name:" + " " + userNameFirst + ", " +
                    "Last Name:" + " " + userNameLast  + ", " +
                 "Phone Number:" + " " + userPhone     + ", " +
                "Rolls of Film:" + " " + userFilm      + ", " + 
             "Number of Prints:" + " " + userPrints;
        }
    }

  4. #24
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    If I told you I was using the String Tokenizer for this project, that would make it possible to display everything without an array, correct?
    Yes, I think you could do it with a StringTokenizer.

  5. #25
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    I'll post here what I have so far, the only problem is that according to what one of the mentors was telling me, the definition class should have ALL the work, the controller class should be a simple message just to show the results, is that correct or even possible?

    As an example we referenced one of my professor's controller classes. It is a program where you create an object (a car) assign it parameters and show it which looks as simple as this:

    Java Code:
    //Create a new object and pass the information
    		//through the constructor instead of getters/setters
    		Car bmw = new Car("BMW", "M6", "RED", 20, 200000.00);
    		
    		//print the object summary
    		JOptionPane.showMessageDialog(null,bmw.toString());
    The problem is for the professor's program he is not asking for input, so following his instructions according to what he wants I got this code so far but I believe it is not correct because I dont have a class to reference in the controller code.

    Java Code:
    public class ProjectDefinition 
    	{	
    		
    		final int USER_EXPOSURES = 1;
    		
    		
    		public static void main(String[] args)
    	   {
    			
    			String input = JOptionPane.showInputDialog
    			(
    					"Welcolme to Photo Express" + 
    					"Please enter the following seperated by spaces:\n\n"
    					+ "First Name:" + "\n" 
    					+ "Last Name:" + "\n"
    					+ "Phone Number:" + "\n"
    					+ "Number of Rolls" + "\n"
    					+ "Number of Prints" + "\n" + "\n"
    					+ "Example:Monkey Luffy 6265554321 5 8"
    					
    					
    				
    			);
    			StringTokenizer st = new StringTokenizer(input);
    			String result;
    			
    			String userFirst = st.nextToken();
    			String userLast = st.nextToken();
    			String userPhone = st.nextToken();
    			String userRolls = st.nextToken();
    			String userPrints = st.nextToken();
    			result ="Thank you for choosing Photo Express, Here is your Summary:" + "\n\n"
    			+ "First Name:" + " " + userFirst + "\n" 
    			+ "Last Name:"+ " " + userLast + "\n"
    			+ "Phone number:" + " " + userPhone + "\n"
    			+ "Number of Rolls:" + " " + userRolls + "\n"
    			+ "Number of Prints:" + " " + userPrints;
    			JOptionPane.showMessageDialog(null, result);
    		
    		
    		}
    	}
    That is my definition, but how would I reference that in the controller class to get it to show up from there?

  6. #26
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Again just to make things clear, here are my professors complete instructions (minus the couple pictures) on what we must accomplish:

    1. You are to write a simple program with two classes. One controller class and a
    class to hold your object definition. (Similar to what we used in class)

    2. Use a package in your project, the package name should be xxxprojectname. xxx
    is your initials taken from the first three characters of your Cal Poly email
    address.

    3. Read in the following from the JOptionPane input window, All data to be entered
    in one line separated by spaces. After the information is read use the
    StringTokenizer to break it into variables:
    a. Name of customer (example: Homer Simpson)
    b. Customer Phone Number (example: 9093873744)
    c. Number of rolls of film to be developed (example: 5)
    d. Number of 4 x 6 prints to be made of each film. (example 8)

    4. Number of Exposures is not entered by user instead it’s a CONSTANT value is 1
    5. Make sure the following is entered correctly, use Try/Catch validation otherwise
    display an error message and close the program:
    a. The name of the customer is not blank
    b. The phone number of the customer is not blank and doesn’t exceed 10
    characters and is an integer with positive number only
    c. The number of rolls is between 1 and 100 and is an positive integer
    d. The number of prints can only be (1,2,8,10, 25,30,50), positive integer
    6. Compute the cost for making pictures using the price of $5.19 for development of
    a roll of film and the price of $0.15 per picture.

    7. Use the following to perform the calculations:
    a. Cost for development = number of rolls * development cost
    b. Cost for printing = number of rolls * number of prints * number of
    exposures * printing cost
    c. Subtotal = Cost for development + Cost for printing
    d. Tax = Subtotal * Tax rate
    e. Total = Subtotal + Tax

    8. Use a while loop to accept multiple orders from different customers and display
    the order summary at the end of each order

    9. Display the following summary using a JOptionPane window

    This is as far as the instructions go concerning the code needed.

  7. #27
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;
    
    public class PC3
    {
        public static void main(String[] args)
        {
            ProjectDefinition3 test = new ProjectDefinition3();
            System.out.println(test);
        }
    }
    
    class ProjectDefinition3
    {
        String userFirst;
        String userLast;
        String userPhone;
        String userRolls;
        String userPrints; 
        final int USER_EXPOSURES = 1;
    
        public ProjectDefinition3()
        {
            String input = JOptionPane.showInputDialog("Welcolme to Photo Express" + 
                   "Please enter the following seperated by spaces:\n\n"
                 + "First Name:" + "\n" 
                 + "Last Name:" + "\n"
                 + "Phone Number:" + "\n"
                 + "Number of Rolls" + "\n"
                 + "Number of Prints" + "\n" + "\n"
                 + "Example:Monkey Luffy 6265554321 5 8");
    
            StringTokenizer st = new StringTokenizer(input);
    
            userFirst = st.nextToken();
            userLast = st.nextToken();
            userPhone = st.nextToken();
            userRolls = st.nextToken();
            userPrints = st.nextToken();
    
            // From you comments above it seems that this next block
            // of code could go into a controller class which you would 
            // instantiate/call from the main method in the PC3 class above.
            String result = "Thank you for choosing Photo Express, " +
                     "Here is your Summary:" + "\n\n"
                   + "First Name:" + " " + userFirst + "\n" 
                   + "Last Name:"+ " " + userLast + "\n"
                   + "Phone number:" + " " + userPhone + "\n"
                   + "Number of Rolls:" + " " + userRolls + "\n"
                   + "Number of Prints:" + " " + userPrints;
            JOptionPane.showMessageDialog(null, result);
        }
    
        public String toString()
        {
            return "First Name:" + " " + userFirst  + ", " +
                    "Last Name:" + " " + userLast   + ", " +
                 "Phone Number:" + " " + userPhone  + ", " +
                "Rolls of Film:" + " " + userRolls  + ", " + 
             "Number of Prints:" + " " + userPrints;
        }
    }

  8. #28
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    hardwired, you truly are.
    Thanks so much man, I had it correct but I just couldnt figure out how to split it so I was gonna submit this code I was working on after I added the calculations and whatnot:

    Java Code:
    public class ProjectDefinition 
    	{	
    		
    		final int USER_EXPOSURES = 1;
    		
    		
    		
    		
    		public static void main(String[] args)
    	   {
    			String choice = "y";
    			while (choice.equalsIgnoreCase("y"))
    		{
    			try{
    				String input = JOptionPane.showInputDialog
    			(
    					"Welcolme to Photo Express" + 
    					"Please enter the following seperated by spaces:\n\n"
    					+ "First Name:" + "\n" 
    					+ "Last Name:" + "\n"
    					+ "Phone Number:" + "\n"
    					+ "Number of Rolls" + "\n"
    					+ "Number of Prints" + "\n" + "\n"
    					+ "Example:Monkey Luffy 6265554321 5 8"	);
    			
    			StringTokenizer st = new StringTokenizer(input);
    			String result;
    			
    			String userFirst = st.nextToken();
    			String userLast = st.nextToken();
    			String userPhone = st.nextToken();
    			int userPhoneTwo = Integer.parseInt(userPhone);
    			if (userPhone.length() > 10 || userPhoneTwo < 0)
    			{ JOptionPane.showMessageDialog(null, "Phone number cannot be longer than 10 digits and must be a positive integer");
    			System.exit(0);}
    		
    			String userRolls = st.nextToken();
    			int userRollsTwo = Integer.parseInt(userRolls);
    			if (userRollsTwo < 1 || userRollsTwo > 100)
    			{ JOptionPane.showMessageDialog(null, "Number of Rolls must be positive between 1 and 100");
    			System.exit(0);}	
    				
    			
    			String userPrints = st.nextToken();
    			result ="Thank you for choosing Photo Express, Here is your Summary:" + "\n\n"
    			+ "First Name:" + " " + userFirst + "\n" 
    			+ "Last Name:"+ " " + userLast + "\n"
    			+ "Phone number:" + " " + userPhoneTwo + "\n"
    			+ "Number of Rolls:" + " " + userRollsTwo + "\n"
    			+ "Number of Prints:" + " " + userPrints;
    			JOptionPane.showMessageDialog(null, result);
    			choice = JOptionPane.showInputDialog(null, "Continue? (y/n)");
    			
    	
    			}
    			catch(NoSuchElementException e)
    			{
    				JOptionPane.showMessageDialog(null, "You must fill in all information");
    			}
    		
    		
    		}
    		
    		
    	
    	  }
    	}

  9. #29
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    One Problem my friend,

    I modified the controller class to say use JOptionPane instead of System.out.println like so:

    Java Code:
    {
        public static void main(String[] args)
        {
            ProjectTwoDefinition test = new ProjectTwoDefinition();
            JOptionPane.showMessageDialog(null, test);
        }
    }
    And everything works as normal but it causes an extra joptionpane window to show up with what ive input all in one line, is there a way to move the summary window that shows everything i input in that long box to the controller?

  10. #30
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Also I'm trying this statement under userPhone token but I can only do the first if not the second part successfully, i think they are in conflict, Im trying to satisfy the part where the phone number cannot be over 10 characters and must be an INTEGER and POSITIVE

    Java Code:
    int userPhoneTwo = Integer.parseInt(userPhone);
    			if (userPhone.length() > 10 || userPhoneTwo < 0)
    			{ JOptionPane.showMessageDialog(null, "Phone number cannot be longer than 10 digits and must be a positive integer");
    			System.exit(0);}

  11. #31
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    is there a way to move the summary window that shows everything i input in that long box to the controller?
    I think you'll have to get the design recommendation from your mentor/teacher:
    according to what one of the mentors was telling me, the definition class should have ALL the work, the controller class should be a simple message just to show the results.
    I have no idea what they have in mind.

    trying to satisfy the part where the phone number cannot be over 10 characters and must be an INTEGER and POSITIVE
    The first part looks okay. For the second part you'll have to parse the string to an int so you can work with it.
    Java Code:
    int phoneNumber = Integer.parseInt(userPhoneTwo);

  12. #32
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    I dont quite understand, the userPhoneTwo is already parsed to an integer in that first line

    int userPhoneTwo= Integer.parseInt(userPhone)

    I even tried seperating the if statements so that first the statement

    if (userPhone.length < 10) shows up and gets checked and THEN I parse userPhone into an integer userPhoneTwo and THEN i check for userPhoneTwo to be a positive but I get an exception?

  13. #33
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I dont quite understand, the userPhoneTwo is already parsed to an integer in that first line
    Yes, you're right. Sorry, I didn't look closely enough before answering.
    Java Code:
    public class Test {
        public static void main(String[] args) {
            // b. The phone number of the customer is not blank and
            //    doesn’t exceed 10 characters and is an integer
            //    with positive number only
            String userPhone = "1234567890";
            // Tests for blank phone number:
            // userPhone.equals("") _or_ userPhone.length() == 0
            // Test for integer value:
            // userPhone.indexOf(".") == -1
            boolean hasDecimal = userPhone.indexOf(".") != -1;
            if(hasDecimal)
                JOptionPane.showMessageDialog(null, "Not an integer value");
            // Or you could catch a NumberFormatException in parsing the string.
            System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE);
            // Eleven digit numbers are too big for an [i]int[/i] size
            // so use [i]long[/i] data type. Or you could catch an exception
            // in parsing the string as an [i]int[/i].
            long phoneNumber = Long.parseLong(userPhone);
            System.out.printf("userPhone = %s  phoneNumber = %d%n",
                               userPhone, phoneNumber);
            int length = userPhone.length();
            if (length == 0 || length > 10 || phoneNumber < 0) {
                JOptionPane.showMessageDialog(null, "Phone number cannot be " +
                         "longer than 10 digits and must be a positive integer");
                System.exit(0);
            }
        }
    }

  14. #34
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Thanks again hardwired, im gonna try and finish this up today with calculations and all to turn in tomorrow, I really appreciate your help, send me a PM with your paypal since I was gonna buy someone lunch so I can just send you a few bucks for your troubles instead.

  15. #35
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Also, I tried putting to exceptions under one try statement, but it didnt seem to work? For example if you look at my code, under the NoSuchElementException e I added a NullPointerException statement

    catch (NullPointerException e) right under that catch and in the braces I put an error hoping it would display for both, do I have to do this differently?

  16. #36
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Error: no class definition found
    By toby in forum New To Java
    Replies: 6
    Last Post: 08-28-2011, 10:32 PM
  2. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:03 PM
  3. Eclipse- jump to method definition
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 03:34 PM
  4. Eclipse - jumping to method definition
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 02:52 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
  •