Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2

    [Homework] Java Project for Comp. Sci. Class. Don't know what to do :(


    Hello!

    I'm a first year college student enrolled in a Computer Science Class. CMPSCI111 - Introduction to Algorithms & Java Programming.

    My class requires that I complete projects that my professor assigns & so far, I've aced the first two.

    Last Thursday, he assigned our Project #3.
    I took a look at it, and I honestly have no clue what to do. I know some things I need in the program are loops, if/else statements, a switch, but I just don't understand how to set up this program. Maybe it's because I don't understand the instructions itself...I don't know.

    The Project instructions:

    "Phase 1: You must design 3 algorithms, and provide both a flow chart and pseudo code for the algorithms.


    Algorithm descriptions:

    Given an integer parameter named current_number and two constant global variables:
    final static int MIN_NUMBER = 1;
    final static int MAX_NUMBER = 8;
    Create an algorithm named forward, that will advance ONE value through a sequence of numbers 1, 2, 3 ... MAX_NUMBER. In other words, when passed a value of 3 in the parameter current_number, it simply returns a 4.

    However, when MAX_NUMBER is reached the algorithm should wrap around back and return MIN_NUMBER. The algorithm will NEVER return a value larger than MAX_NUMBER.

    Create an algorithm named backward, that will move through a sequence of numbers ... 3, 2, MIN_NUMBER. In other words, when passed a value of 6 in the parameter current_number, it simply returns a 5.

    When MIN_NUMBER is reached the algorithm should STOP and return the value MIN_NUMBER. The algorithm will NEVER wrap around.

    Create an algorithm named createFileName, that takes a number as input, current_number, and builds and returns a String like "pictureX.gif", where X is the value in the input parameter.

    This should fit on 1 sheet of paper. Place the 3 flowcharts (one per method) on one side of the paper and the matching pseudo code next to it or on the other side.

    Good luck.

    Phase 2: Implement your algorithms with three methods for this phase. The methods should behave as described in phase 1. The forward method should wrap around when it reaches the last number, the backward method should stop when it reaches the first number.
    Add the MIN_NUMBER and MAX_NUMBER constants at the top of your class, you will also need to add a global variable at the top of your class:
    static int image_number = 1;
    The forward() and backward() methods MUST use an input parameter and output a return value, they DO NOT use the above global variable image_number directly, but they should use the constants . The methods are very simple, when the current image number is 3 forward changes it to a 4, when the current image number is 6 backward() changes it to a 5. The method createFileName() will take a number as input, and returns a String containing a file name like "pictureX.gif".

    Implement the following methods

    Code:
    public static int  forward ( int current_number ) {
        // return the new image number
    }
    
    public static int  backward ( int current_number ) {
        // return the new image number
    }
    
     // use the constants MIN_NUMBER, MAX_NUMBER, do not use hard coded 1 or 8
    
    
    public  static String  createFileName ( int current_number ) {
        // return a filename like pictureX.gif
    }
    
    public static String  createRandomName (  ) {
        // return a filename like pictureX.gif
    	// using a RANDOM number between MIN_NUMBER and MAX_NUMBER 
    }
    
    public static void  showMenu (  ) {
        // write a loop
        // Display a menu, with options 1 .. N for each method above, and an exit option
    	// get user input and call the correct method using a switch
    	// print out the NEW image number everytime the value changes
    }
    
    public static void main ( String [] args) {
      //  call showMenu
      
    }
    The menu should have options for calling forward(), backward(), createFileName () and createRandomName ( ). Be sure to use parameters when calling the functions, then use the return value to update the global image number. At this point, your program will show the menu, call the methods and print onto the console the new image number everytime it changes.

    Phase 3: Add a GUI window to display the graphics file names the phase 2 methods create. Study the GUI programs in chapter 7, listings 7.3 and 7.4
    The first requirement in writing a GUI program is you must import the GUI packages at the top of your code.

    Code:
     import javax.swing.*;
      import java.awt.*;
      import java.net.*;
    This will add the classes to your program needed for windows, graphics and loading images over the Internet.
    Write a new function to create and display a GUI window:

    Code:
    public static void showWindow( String filename ) {
        // create, size and show a GUI window frame, you may need to click on taskbar to see window
    	//display the filename in the title of the window frame, otherwise the window will be blank (for now)
      }
    In your switch statement, where you call the methods createFileName() and createRandomName() add calls to showWindow() and pass it the file name the methods return. Test your program, you should be able to go forward and backward with proper behavior when limits are reached. When the window is displayed the correct file name should be shown in the frame. Do not continue to phase 4 until you are sure the program works.

    Phase 4: Static vs. Non-Static Methods
    A static method does NOT need an object to execute. A non-static method REQUIRES an object to execute. Creating an object is simple, inside of the showMenu method:

    Code:
    ClassName theobject = new ClassName();   // E.G.   public class ClassName   {   ....
    With this variable you can now call non-static methods using the dot '.' operator:

    Code:
    theobject.method();   // E.G.   call a method that is NON-static using an object
    Now we need to create some non-static methods! Overloaded methods are two methods with the SAME name. We can create a method with the same name as long as it has a different number or type of parameters. Add the following OVERLOADED methods to your program:


    Code:
    public  void  forward (  ) {
        // overloaded method, use global variable as input and output
    }
    
    public  void  backward (  ) {
        // overloaded method, use global variable as input and output
    }
    In addition to having different parameters, these NEW functions will operate differently. Instead of using parameter and return type for input and output, these will be allowed to use the image number global variable for input and output. Add two NEW options to your menu to call these new methods. Both versions of forward() and backward() do basically the same thing, but the non-static versions require an object to execute. Test your program, make sure you can move forward and backward using both techniques.

    Phase 5: Time for some graphics! Below is a method to load an image over the Internet. It requires a working Internet connection to properly work (but not to compile). This method should be copy and pasted INSIDE your class and called from showWindow(). This method uses parameters and a return type to get information into and out of the method. Please do not ask the instructor or fellow students how to call this method , you need to know and understand how parameters and return variables work. It is very important that you do this yourself.

    Code:
    // This routine will load an image into memory, non-static requires an object
    // It expects the name of the image file name and a JFrame  passed to it
    // It will assume an Internet conection is available
    // It can only be called AFTER the program object has been created
    // It will return a type Image variable, call it like this:   theimage = object.load_picture("picture1.gif", frame); 
    // (hard code 'picture1.gif' only when testing - USE a method or variable for 'real' call)
    // This code requires you to do an 'import java.awt.*' and an 'import java.net.*'
    // Note: this method is using parameter and return type for input/output
    
    public Image load_picture(String imagefile, JFrame theframe)
    {
    
    Image tempimage;
    // Create a MediaTracker to inform us when the image has
    // been completely loaded.
    MediaTracker tracker;
    tracker = new MediaTracker(theframe);
    
    // getImage() returns immediately. The image is not
    // actually loaded until it is first used. We use a
    // MediaTracker to make sure the image is loaded
    // before we try to display it.
    
    String startURL;
    if (imagefile.startsWith("http"))
       startURL = "";
    else
       startURL = "()";
    
    URL myURL=null;
    try
    {
    myURL = new URL(startURL + imagefile);
    }
    catch(MalformedURLException e) {
      System.out.println("Error caught " + e.toString());
    }
    
    //tempimage = getImage(myURL);   // JApplet version
    tempimage = Toolkit.getDefaultToolkit().getImage(myURL); // stand alone program version
    
    
    // Add the image to the MediaTracker so that we can wait for it
    
    tracker.addImage(tempimage, 0);
    try { tracker.waitForID(0); }
    catch ( InterruptedException err) { System.err.println(err); }
    
    return tempimage;
    }
    Next is a class/method that creates a custom JPanel. It must also be placed INSIDE of your class. This class/method uses a global variable to pass information to the method. You must create and set that global variable in order to use this code. Please do not ask the instructor or fellow students how to use a global, you need to know and understand how global variables work. It is very important that you do this yourself.

    You will also need to to look up how to create AND add a JPanel to a JFrame. Review Listings 7.3 and 7.4 If you can create AND add a JPanel, then all you need to do is substitute 'MyPanel' for the class name 'JPanel' and this code will display an image on the window frame.


    Code:
    // This class/method uses a  global variable that MUST be set before calling/using
    // note: You can not call the paint routine directly, it is called when frame/window is shown
    // Review Listings 7.3 and 7.4
    //
    public static class MyPanel extends JPanel {
     public void paintComponent (Graphics g) {
        int xpos,ypos;
        super.paintComponent(g);
        // set the xpos and ypos before you display the image
        xpos = 10; // you pick the position
        ypos = 10; // you pick the position
        if (theimage != null) {
            g.drawImage(theimage,xpos,ypos,this);
            // note: theimage global variable must be set BEFORE paint is called
        }
     }
    }
    One last thing, when you have the program operating properly create one more method and add a menu option to call it:
    Code:
    public String getAnyImage()
    This method should ask the the user to enter a full URL of any image located on the Internet (e.g. '(URL)) and return the full URL. After the method returns, call showWindow() and pass it the full URL for display.




    I tried skipping Phase 1 and went straight on to Phase 2 because a lot of people in my class did that and I think they're doing fine, and some of the code I wrote works, but I'm missing A LOT. I'm currently working on Phase 1 right now but I'm stuck with making the flow charts and I feel like I'm missing some parts for my pseudo code. I especially have no clue how to make a menu (although I know it involves a switch..I think).

    I need to complete all 5 phases of this project by next Thursday because that's when my professor starts grading it.
    I've been trying to ask him for help, but he's been so busy grading the rest of the class on our previous project.

    So could someone please help me? I'd greatly appreciate it!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit your post and wrap the code in code tags to preserve formatting.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    Originally Posted by NormR
    Please edit your post and wrap the code in code tags to preserve formatting.
    Like that? I think I did it right. Please correct me if I'm wrong. I'm new to these forums.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    Ok so I started working on the forward & backward part of the project and so far, I think it'll work.

    Here is part of the code I started working on:
    Code:
    import java.util.Scanner;
    
    public class PictureViewer {
    
        final static int MAX_NUMBER = 8;
        final static int MIN_NUMBER = 1;
        
        public static void main(String[] args) {
        }
    
        public static int forward(int current_number) {
            Scanner input = new Scanner(System.in);
    
            System.out.print("Enter integer: ");
            current_number = input.nextInt();
    
    
            if (current_number >= MAX_NUMBER) {
                System.out.println(MIN_NUMBER);
            } else {
                System.out.println(current_number + 1);
            }
            return current_number;
        }
    
        public static int backward(int current_number) {
            Scanner input = new Scanner(System.in);
    
            System.out.print("Enter integer: ");
            current_number = input.nextInt();
    
            if (current_number <= MAX_NUMBER) {
                System.out.println(current_number--);
            }
            else if (current_number == MIN_NUMBER){
                System.out.println(MIN_NUMBER);
            }
            return current_number;
        }
    }
    I still have a lot to work on. I need help on how to call a method (because my professor isn't exactly the greatest explainer :/) and I'm trying to create a menu, but I really don't know how. but if anyone could help me out, it would be greatly appreciated!


    Oh the program I'm using to write code is called Netbeans.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I need help on how to call a method
    You have several examples in your code:
    Code:
    System.out.println(MIN_NUMBER);
    
      g.drawImage(theimage,xpos,ypos,this);
    Can you explain what problems you are having?

    See the tutorial:http://docs.oracle.com/javase/tutori...O/methods.html
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    Originally Posted by NormR
    You have several examples in your code:
    Code:
    System.out.println(MIN_NUMBER);
    
      g.drawImage(theimage,xpos,ypos,this);
    Can you explain what problems you are having?

    See the tutorial:http://docs.oracle.com/javase/tutori...O/methods.html
    Well my professor wants us to create a menu using a switch statement that can choose between Forward, Backward, and anything else listed.

    The problems I'm having are: Creating that menu, and mainly everything after the backwards method stated in phase 2.

    For example, I honestly don't understand how the algorithm createFileName is suppose to work after it takes an input number & pretty much everything else after that.

    Does it involve using an array?
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    create a menu using a switch statement
    Print a list of values the user can enter, read the response and use the value in a switch statement.
    The switch statement can be used to select a block of code to be executed based on the value passed to the switch(<theValue>) {}
    The case: statements within the switch statement are used to select one value of theValue.
    See:http://docs.oracle.com/javase/tutori...ts/switch.html
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    ok that helped me a lot thank you!

    I'm not quite done with my code, but here's what I have for my code so far (and it's working how I believe it's suppose to...)


    Code:
    import java.util.Scanner;
    
    public class PictureViewer {
    
        final static int MAX_NUMBER = 8;
        final static int MIN_NUMBER = 1;
        
    
        public static void main(String[] args) {
            showMenu();
        }
    
        public static void showMenu() {
            Scanner input = new Scanner(System.in);
            int current_number = MIN_NUMBER;
    
            boolean Continuation = true;
            while (Continuation) {
    
                System.out.println("Select One of the Options");
                System.out.println("Option 1: Forward");
                System.out.println("Option 2: Backward");
                int Menu_Option = input.nextInt();
                switch (Menu_Option) {
                    case 1:
                        current_number = forward(current_number);
                        System.out.println("The Current Number is: " + current_number);
                        break;
                    case 2:
                        current_number = backward(current_number);
                        System.out.println("The Current Number is: " + current_number);
                        break;
    
                }
            }
        }
    
        public static int forward(int current_number) {
    
            if (current_number >= MAX_NUMBER) {
                System.out.println(MIN_NUMBER);
                current_number = MIN_NUMBER;
            } else {
                System.out.println(current_number++);
            }
            return current_number;
        }
    
        public static int backward(int current_number) {
    
            if (current_number <= MIN_NUMBER) {
                System.out.println(current_number);
                current_number = MIN_NUMBER;
            } else current_number --; {    
            }
            return current_number;
        }
    }
    now the next thing I need help with is how to do the createFileName part.
    what exactly should I do to make it so that my current_number builds and return a String? I'm not quite sure how to do that.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    I actually got that createFileName part of the project.

    I'm having problems with the Random Name generator. How do I do it? The one they give me in my textbook does not work for me or anyone else in my class
  18. #10
  19. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by jron5
    I actually got that createFileName part of the project.

    I'm having problems with the Random Name generator. How do I do it? The one they give me in my textbook does not work for me or anyone else in my class
    What Random Number generator did you use?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    Originally Posted by bullet
    What Random Number generator did you use?
    Code:
    Math.random();
  22. #12
  23. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by jron5
    Code:
    Math.random();
    Remember that is going to give you a random number somewhere between 0.0 and 1.0 with 1.0 excluded.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    Originally Posted by bullet
    Remember that is going to give you a random number somewhere between 0.0 and 1.0 with 1.0 excluded.
    Oh. So how would I make it so that I could generate a random number between my global variables: MAX_NUMBER = 8 MIN_NUMBER = 1 that would include both of them?
  26. #14
  27. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by jron5
    Oh. So how would I make it so that I could generate a random number between my global variables: MAX_NUMBER = 8 MIN_NUMBER = 1 that would include both of them?
    Note what happens when you multiply a number by Math.random()
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    Originally Posted by bullet
    Note what happens when you multiply a number by Math.random()
    Ok so my random number generator is fixed & I've managed to get to the showWindow part of my project.

    Now I'm stuck on Phase 4, which is utilizing overloaded methods. How is this suppose to work? I tried using something similar to my forward and backwards method, but instead I replaced the parameters w/ the global variables. But I'm getting a huge error message.
    In addition to that, I also tried calling one of those overloaded methods, but for some reason in my coding, it automatically directs me to my first forward method.

    Here is my updated code:
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.util.Scanner;
    import java.util.Random;
    import javax.swing.*;
    import java.awt.*;
    import java.net.*;
    
    public class PictureViewer {
    
        final static int MAX_NUMBER = 8;
        final static int MIN_NUMBER = 1;
        static int image_number = 1;
        static String FileName;
        static Random randGen = new Random();
    
        public static void main(String[] args) {
            showMenu();
        }
    
        public static void showMenu() {
            Scanner input = new Scanner(System.in);
            int current_number = MIN_NUMBER;
            PictureViewer object = new PictureViewer();
            boolean Continuation = true;
            while (Continuation) {
    
                System.out.println("Select One of the Options");
                System.out.println("Option 1: Forward");
                System.out.println("Option 2: Backward");
                System.out.println("Option 3: Create File Name");
                System.out.println("Option 4: Create Random Name");
                System.out.println("Option 5: Show Window");
                System.out.println("Option 6: ");
                System.out.println("Option 7: ");
                System.out.println("Option 8: ");
                System.out.println("Option 9: Exit");
                int Menu_Option = input.nextInt();
                switch (Menu_Option) {
                    case 1:
                        current_number = forward(current_number);
                        System.out.println("The Current Number is: "
                                + current_number);
                        break;
                    case 2:
                        current_number = backward(current_number);
                        System.out.println("The Current Number is: "
                                + current_number);
                        break;
                    case 3:
                        FileName = createFileName(current_number);
                        System.out.println("File Name: " + FileName);
                        break;
                    case 4:
                        FileName = createRandomName(current_number);
                        System.out.println("File Name: " + FileName);
                        break;
                    case 5:
                        showWindow(FileName);
                        break;
                    case 6:
                            
                        break;
                    case 7:
    
                        break;
                    case 8:
                        
                        break;
                    case 9:
                        Continuation = false;
                        System.out.println("Goodbye!");
                        break;
                }
            }
        }
    
        public static int forward(int current_number) {
    
            if (current_number >= MAX_NUMBER) {
                System.out.println(MIN_NUMBER);
                current_number = MIN_NUMBER;
            } else {
                System.out.println(current_number++);
            }
            return current_number;
        }
    
        public static int backward(int current_number) {
    
            if (current_number <= MIN_NUMBER) {
                System.out.println(current_number);
                current_number = MIN_NUMBER;
            } else {
                current_number--;
            }
            return current_number;
        }
    
        public static String createFileName(int current_number) {
    
            return "picture" + current_number + ".gif";
        }
    
        public static String createRandomName(int current_number) {
            current_number = MIN_NUMBER + (int) (Math.random() * MAX_NUMBER);
            return "picture" + current_number + ".gif";
        }
    
        public static void showWindow(String filename) {
            JFrame frame1 = new JFrame();
            frame1.setTitle(FileName);
            frame1.setSize(400, 400);
            frame1.setLocation(200, 100);
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setVisible(true);
        }
    
    //    public void forward() {
    //        if () {
    //            
    //        } 
    //        else {
    //        }
    //    }
    //
    //    public void backward() {
    //        overloaded method, use global variable as input and output
    //    }
    }
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo