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