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

Thread: Need Help Altering My Application, Please...

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

    Default Need Help Altering My Application, Please...

    Hi everyone,

    I've been coding for a few weeks now through a class and I'm still struggling a bit with it. I'm prepping for an exam and my teacher gave us a practice test (but wont give answers) to prep. I've been stuck on one of the questions for days and the class tutors haven't been a help.

    The instructions are:
    Alter the code to allow the user to input ANY numbers for a box to flip. For example, if the user inputs 2,5,100, your program should print: This range is out of bounds. Please choose a range that is within (0,0) and (width ,height ), where width and height are the actual width and height of the picture chosen. If the user enters constraints that will not cause an out of bounds error, then flip that portion of the picture and continue. If the user enters constraints that will cause an out of bounds error, then reprompt them. This should not count towards the 3 flips for each vertical and horizontal. There should still be a total of 6 FLIPS; 3 horizontal, 3 vertical.

    The instructions were confusing for me at first, so to put it simply, alter the code so rather than get an out of bounds error, the user is reprompted.

    The code is:
     /* Choosing a picture and initializing variables. */ 
        Picture pic = new Picture(FileChooser.pickAFile());
        int x, y, size; pic.show();
        int width = pic.getWidth(); 
        int height = pic.getHeight(); 
        int index = 0; 
        System.out.println("Picture loaded - width: " + pic.getWidth()+" height: "+ pic.getHeight());
        for (index = 0; index < 3; index++)
        { 
          /* Prompting the user for coordinates. */ 
          String prompt = "Please enter a position (first x, then y) in the ";
          String prompt2 = "picture to flip horizontally.";
          System.out.println(prompt + prompt2);
          /* Converting coordinates to ints. */
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          x = (new Integer(br.readLine())).intValue();
          y = (new Integer(br.readLine())).intValue();
          /* Prompting the user for size. */ 
          System.out.println("Please enter the size of the box to flip.");
          size = (new Integer(br.readLine())).intValue(); 
          pic.flipHorizontal(x, y, size); 
          /* Repainting the picture with the horizontal flips completed. */ 
          pic.repaint();
        } 
        for (index = 0; index < 3; index++)
        {
          /* Prompting the user for coordinates. */ 
          String prompt = "Please enter a position (first x, then y) in the "; 
          String prompt2 = "picture to flip vertically."; 
          System.out.println(prompt + prompt2); 
          /* Converting coordinates to ints. */ 
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
          x = (new Integer(br.readLine())).intValue(); 
          y = (new Integer(br.readLine())).intValue(); 
          /* Prompting the user for size. */ 
          System.out.println("Please enter the size of the box to flip."); 
          size = (new Integer(br.readLine())).intValue();
          pic.flipVertical(x, y, size); 
          /* Repainting the picture with the horizontal flips completed. */ 
          pic.repaint(); 
          }
        }
      }

    Please let me know if there is anything else you need (such as the method used, any additional information, etc). I'll try to provide the necessary information to the best of my ability


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Need Help Altering My Application, Please...

    First, do you have to use BufferedReader for your text input? The Scanner class seems to be a little bit easier for this.

    Now, he wants you to include a way to check the input for the legal bounds, right? So how do you check? You can use an if/then statement after you input the values, right? Or you can use a while loop that runs until the values are correct.

    For example:
     
    Scanner a = new Scanner(System.in);//input
     
    		System.out.print("Input a number less than 10: ");//prompt
    		while(Integer.parseInt(a.next())>=10)//gets and checks the input in a loop
    		{
    			System.out.print("Input a number less than 10: ");//re-prompts if need be.
    		}

    This is a good example of something that prompts the user for an input, and will keep re-prompting until a certain condition is met, in this case until a number less than ten is inputted. The check for you may be more complex, and may need some equations to run, but it is fairly easily done.

Similar Threads

  1. Charset problems when altering a pdf-file
    By jakobmann in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 7th, 2012, 06:02 AM
  2. how to run any installed application through my java application??
    By sgsamanthjain in forum Java Theory & Questions
    Replies: 1
    Last Post: April 1st, 2011, 09:17 AM
  3. Replies: 2
    Last Post: March 23rd, 2011, 09:51 AM
  4. [SOLVED] [help] the application file (.jad) for application does not appear to be the ....
    By ordinarypeople in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2010, 04:50 AM
  5. Replies: 0
    Last Post: December 3rd, 2009, 04:43 PM

Tags for this Thread