Question: How do I design 3 algorithms, and provide both a flow chart and pseudo code for the algorithms. Algorithm descriptions: Given an integer parameter named
How do I 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. This 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. this is the code that I have but I don't know how it should look as a pseudo code or as a flow chart: import java.awt.image.BufferedImage; import java.net.URL; import java.util.Random; import java.util.Scanner; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class PictureViewer { final static int MIN_NUMBER = 1; final static int MAX_NUMBER = 8; static int image_number = 1; // Add your methods here public static void main(String[] args) { showMenu(); } public static int forward(int current_number) { if (current_number == MAX_NUMBER) { current_number = 1; } else { current_number++; } return current_number; } public static void forward() { if (image_number == MAX_NUMBER) { image_number = 1; } else { image_number++; } } public static int backward(int current_number) { if (current_number == MIN_NUMBER) { return current_number; } else { current_number--; } return current_number; } public static void backward() { if (image_number == MIN_NUMBER) { image_number = MIN_NUMBER; } else { image_number--; } } public static String createFileName(int current_number) { String fileName = "picture" + current_number + ".jpg"; return fileName; } public static String createRandomName() { Random rand = new Random(); int randomNum = rand.nextInt(MAX_NUMBER) + MIN_NUMBER; String randomName = "picture" + randomNum + ".jpg"; return randomName; } public static void showWindow(String filename) { JPanel myPanel = new JPanel(); JFrame pictureFrame = new JFrame(); pictureFrame.setTitle(filename); pictureFrame.setSize(800, 600); pictureFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myPanel.add(load_picture(filename)); pictureFrame.add(myPanel); pictureFrame.setVisible(true); } public static JLabel load_picture(String imagefile) { JLabel templabel = null; String startURL = ""; if(!imagefile.startsWith("http")) startURL = "http://riveira.x10host.com/CMPSCI111L/images/"; URL myURL = null; try { myURL = new URL(startURL + imagefile); BufferedImage myPicture = ImageIO.read(myURL); templabel = new JLabel(new ImageIcon(myPicture)); } catch(Exception e) { System.out.println("Error caught " + e.toString()); } return templabel; } public static void showMenu() { boolean inMenu = true; Scanner input = new Scanner(System.in); while (inMenu == true) { System.out.println("Menu" + " 1. Forward" + " 2. Overload Forward" + " 3. Backward" + " 4. Overload Backward" + " 5. Create File Name" + " 6. Create Random Name" + " 7. Quit"); System.out.print("Selection: "); int selection = input.nextInt(); switch (selection) { case 1: image_number = forward(image_number); System.out.println("image_number: " + image_number); break; case 2: forward(); System.out.println("image_number: " + image_number); break; case 3: image_number = backward(image_number); System.out.println("image_number: " + image_number); break; case 4: backward(); System.out.println("image_number: " + image_number); break; case 5: String fileName = createFileName(image_number); showWindow(fileName); break; case 6: String randomName = createRandomName(); System.out.println(randomName); break; case 7: inMenu = false; break; } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
