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

Thread: need help on an assignment :(

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help on an assignment :(

    Hello! I'm in an introductory programming class for Java, and admittedly kinda lost. I was recently assigned the following project:

    Create the class DataStore. This class will store and perform calculations on user entered data.

    Use commenting to provide a general description of the class, including your name, section, assignment number, and the due date. Following, use commenting to describe the logic behind important elements of the class.

    Here are the details of the class:
    Two properties of type ArrayList will be used to store a list of String and Integer values. Recall our principles of encapsulation, these will be our only properties. Any other variables you need will be encapsulated (declared and used) within an individual method.
    You will provide a method menu(), which will print a menu of operations, allowing the user to make a selection, and continue to cycle until the user selects the quit operation. Menu operations are: to add a String and Integer value to the lists (each time this option is selected a single String and a single Integer will be added), print the String and Integer values one per line, and print the Integer value multiplied by the index position of the Integer within the list (start your index counting at zero, so the first position will have its value multiplied by zero)
    Other structural elements the class will contain:
    Our standard list of methods.
    The method addStringAndInteger().
    The method printMultiplications().
    The method printInfo().

    Sample Output
    Welcome to the Data Store

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: 2

    All Information Stored

    String Integer

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: 1

    Enter a String Value: First Entry
    Enter a Integer Value: 123

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: 1

    Enter a String Value: Second
    Enter a Integer Value: 213

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: 1

    // ... repeat entering values

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: 2

    All Information Stored

    String Integer
    First Entry 123
    Second 213
    //...

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: 3

    Multiplications

    0 * 123 = 0
    1 * 213 = 213
    //...

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: // ... continue to perform operations

    (1) Add a String and Integer
    (2) Print All Information
    (3) Print Multiplications
    (4) Quit

    Selection: 4

    Thank you for using the Data Store.


    I can tell that the solution requires a loop that will only keep running as long as (int selection != 4), but to be honest that's all I can really figure out atm and the more research I do the more confused I get. I don't expect anybody to do my hw for me, but any advice you have would be greatly appreciated! Thank you, my professor looks like Craig T. Nelson.


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: need help on an assignment :(

    Here's some pseudocode for what you want, I believe. I'll leave the details and the translation to you...

    DataStore()
    1. continue := true
    2. while continue
    3.    do PrintMenu()
    4.    selection := ReadSelection()
    5.    if selection = 1 then AddStringAndIntegerPair()
    6.    else if selection = 2 then PrintStringAndIntegerPairs()
    7.    else if selection = 3 then PrintMultiplications()
    8.    else continue = false
     
    PrintStringAndIntegerPairs()
    1. for i = 1 to len(store)
    2.    do (string, integer) := store[i]
    3.    print (string, integer)
     
    PrintMultiplications()
    1. for i = 1 to len(store)
    2.    do (string, integer) := store[i]
    3.    print (integer * i)
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

  3. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: need help on an assignment :(

    i always find it easiest to find out what problems i would need to
    figure out b4 i actuly make it so ...

    problems that would need to be solved

    create an array that dosent have a set limit ( easy if u know how )
    make the program run until u quit ( easy if u know how )
    and read and validate numerical input from the user ( medium for newer programmers )

    main runs method [top]
       top reads & validates input then runs 1 of the others [ add, printAll, multiply,quit ]
         add reads & validates the inputed info then stores it and returns to [top]
         printAll runs a loop to print all info then returns to [top]
         multiply reads & validate the input and gets the numbers to be multiplied then displays answer and returns to [top]
         quit is self explanitory XP
    Programming: the art that fights back

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help on an assignment :(

    To learn the java syntax, I'd suggest Java nuts and bolts: Control Flow Statements to understand how to write for, while, and if/else statements to control the flow of your program.

    A good idea would be to use helper methods. These are basic methods that will do small (or not small) tasks based on a list of parameters you pass to that method.

    // declaring a simple helper method
     
    public static void main(String[] args)
    {
         printoutMenu(); // our helper method to print out a menu everytime it is called
    }
     
    public static void printoutMenu() // this line is the method signiture
    {
         System.out.println("This is a simple menu");
         System.out.println("1. Hello world!");
         System.out.println("2. Hello moon!");
         System.out.println("3. Goodbye!");
    }

    To pass parameters to a method, simply add variable declarations to the method signature between the two parenthesis:

    public static void printInt(int n) // an integer called n will be passed to this method. It will be treated like a local variable
    {
         System.out.println("The integer was " + n);
    }

    To get a value back from a method, modify the method signature to have a return type (note: void means no return value).

    public static [b]int[/b] getInt() // the stuff in bold is the return type
    {
         return 3; // when this method gets called, this value will get returned
    }
     
    public static [b]double[/b] getDouble(boolean isRandom)
    {
         if (isRandom)
         {
              return Math.random(); // a random double generated using Math.random
         }
         else
         {
              return 0.0;
         }
    }

    It is important to note that you can only return one thing from a method each time it is called, but each time that method is called the value returned doesn't necessarily have to be the same. You can also have multiple return statements in a method, so long as every control path ends with exactly one return value (note that methods with a void return type don't necessarily need to have a return statement at the end, it's understood that it will return nothing once the end of the method is reached).

  5. #5
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: need help on an assignment :(

    @ hello
    their is a work around for multiple non object returns
    just use a string array if its mixed , or if its just numbers then a int array would work
    all u have to do is add a loop to read the info from the array after it returns

    [note] i try finding work arounds for everything i can XD
    Programming: the art that fights back

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help on an assignment :(

    Regardless, you are only allowed to return one thing. But yes, what you return doesn't matter, so you could create an object to hold lot of different information and return that single object.

    The other work-around is to use the pass by reference trick (only works with objects). This will allow you to modify any number of objects passed by reference to your method. However, there is one limit: you must operate on the object passed, you can't create a new object and try to change the reference to this new object (well, you could but if you do that new object will behave just like a local variable)

    public void swap(int[] nums)
    {
         // assume nums is an integer array of size 2
         int temp = nums[0];
         nums[0] = nums[1];
         nums[1] = temp;
    }
     
    public static void main(String[] args)
    {
         int[] nums = {1,2};
         System.out.println("The current order: " + nums[0] + " " + nums[1]);
         swap(nums);
         System.out.println("The swapped order: " + nums[0] + " " + nums[1]);
    }

    [/work-around to the work-around]

  7. #7
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: need help on an assignment :(

    Quote Originally Posted by helloworld922 View Post
    [/work-around to the work-around]
    with a healthy side of pownage XD XP
    Programming: the art that fights back

Similar Threads

  1. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  2. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 09:44 PM
  3. New assignments for JAVA beginners
    By Peetah05 in forum File I/O & Other I/O Streams
    Replies: 24
    Last Post: April 24th, 2009, 11:33 AM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  5. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM