Menu

(Solved) : Got Algorithms Cant Seem Implement Code Right Try Invoke Forward Backward Methods Switch C Q37174433 . . .

I got my algorithms down but i cant seem to implement them intothe code right. When i try to invoke the forward or backwardmethods in my switch case for the menu i just get a bunch oferrors

package project4;

import java.awt.image.BufferedImage;
import java.net.URL;
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;
public static int forward(int current_number) {
if (current_number<MAX_NUMBER){
current_number++;}
else if (current_number>MAX_NUMBER) {
return MIN_NUMBER;}
return current_number;
}

public static int backward(int current_number) {
if(current_number>MIN_NUMBER){
current_number–;}
else if (current_number<=MIN_NUMBER){
return MIN_NUMBER;}
return current_number;
}
  
public static String createFileName(int current_number) {
String imageString= “picture”+current_number + “.jpg”;
return imageString;
}
public static String createRandomName() {
int random_number= 1 + (int)(Math.random() * (MAX_NUMBER -MIN_NUMBER) + 1);
String imageString= “picture” +random_number + “.jpg”;
return imageString;
}
  
public static void showMenu() {
Scanner input=new Scanner(System.in);
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”);
  
switch case goes here but im not sure how to invoke the methodswithout getting errors
}

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;
}
}

Phase 1 (3 points) Design your methods in this phase, we will revisit the concept of an algorithm. Remember, an algorithm isPhase 2 (12 points). Implement your algorithms in this phase, you will im attached PictureViewer class your own methods to thctrl public static void showMenu () //write a loop 1. Inside the loop, display a menu with options 1..N for each of the eache static void forward() pub overloaded method. This method is allowed to // use the global image number variable for both / iPhase 4 (6 points) im es d some code to invoke the existing showwindow) method, pass the filenames the createFileNamel) and c

Phase 1 (3 points) Design your methods in this phase, we will revisit the concept of an algorithm. Remember, an algorithm is a detailed set of instructions which describe how you intend to solve a specific problem in a finite amount of time Therefore, to complete this phase, you must design 3 algorithms. Write out both a flow chart and pseudocode for the algorithms, then show these algorithms to me in lab class. Here is a description of each algorithm: Given an integer-type parameter named current number, a global integer variable named image number, and two global symbolic constants: final static int MIN NUMBER 1: final static int MAX NUMBER 8: static int image-number = 1; Write an algorithm named forward which increases the value passed to its parameter current number by one each time the algorithm is invoked. In other words, when forward is invoked and passed a number like 1, then forward returns a value of 2. When forward is invoked and passed a value of 2, then forward returns a value of 3. Hówever, when MAX NUMBER is reached, value larger than MAX NUMBER. Write an algorithm named backward which current_number by one each time the algorithm is invoked. In other words, when backward is invoked and passed a number like 8, then backward returns a value of 7. When backward and passed a number like 7, then backward returns a value of 6. However, when MIN _NUMBER is reached, the algorithm should STOP and return the value of MIN NUMBER. This algorithm will NEVER “wrap around” decreases the value passed to its parameter is invoked Write an algorithm named createFileName which concatenates and returns a String like createFileName is invoked and passed a number like 8, then createFileName returns the String picture8.jpg. When createFileName is invoked and passed a number like 1, then createFileName returns the String picture1.jpg. pictureX.jpg where X is the value of the input parameter current_number. In other words, when BE SURE TO HAV EME CHECKANDAPPROVEYOURALGORİTHMSBEFOREPROCEEDINGTO PHASE 2. NO ADDITIONAL POINTS WILL BE AWARDED IF YOU DO NOT SHOW ME YOUR ALGORITHMS Phase 2 (12 points). Implement your algorithms in this phase, you will im attached PictureViewer class your own methods to this class. Your methods should behave as described in your that is, the forward()method should “wrap around” when it reaches plement your algorithms in an actual Java class by adding six methods to the Do not modify the existing code in the PictureViewer class, simply add Phase 1 aigorithms, it reaches the last number, the kward() method should STOP when it reaches the first number, and the createFi1elame) method should return the String picture8.jpg when the method is invoked and passed a number like B Your forward () and backward () methods MUST use a parameter named current number for input and MUST return an int-type value, they MUST NOT use the global image_number variable directly. Also, both methods SHOULD use the symbolic constants MIN NUMBER and MAX NUMBEP instead of the hardcoded, literal numbers 1 and 8. Don’t try to make the methods overly complex, they should be very simple; when forward 0 is passed a current_number of 3, the method returns a 4. When backwardl) is passed a current number of 6, the method returns a 5. The createFileName() method will use a parameter named current _number for input, and will return a String representing a filename like pictureX.jpg, where X is the value of current number. Use the following method headers: public static int forward(int current number) public static int backward(int current _number) // The static methods above should use the symbolic constants // return the new number // return the new number MIN NUMBER, MAX NUMBER. Do not use hardcoded 1 or 8 public static string createFileName (int current number) // return a String representing a filename like picturex.jpg public static String createRandomName () f // Return a String representing a filename 1ike picturex.jpg // where X is a random number between MIN NUMBER and MAX NUMBER ctrl public static void showMenu () //write a loop 1. Inside the loop, display a menu with options 1..N for each of the each of the methods above as well as an exit option 2. Print the value of the global image number variable 3. Prompt the user for a menu option choice 4. Get the user’s menu option choice, then invoke the correct method using a SWITCH. For example, if your menu shows option 1. Forward, and the user’s menu option choice is 1, the switch should then invoke the forward) method public static void main (Stringl) args) // Invoke showMenu () here // Learning how to invoke a method is crucial to this chapter, // so DO NOT ask a fellow student or even the instructor how // to do this pictureX.jpg, where X is a random number between MIN NUMBER and MAX NUMBER, inclusive The menu displayed by showMenu() should have options for invoking forward/). backward). global image n to use ) and createFileNamel) methods. to update image_number by assigning it the numbers returned by the forward) or ) method. Also, when the exit option is chosen in showMenu(), use System.exit (0) to terminate the program. At this point, your program should display a menu, invoke methods and print the image number in the NetBeans console output window Phase 3 (9 points) overloaded methods. An overloaded method is one that has We can create a method with the same name as another, as long This phase will require you to write two the SAME NAME as another method. as it has a different list of parameters. The difference must be in either the number of the data types of those parameters. Add the following OVERLOADED methods to your PictureViewer class: parameters or in e static void forward() pub overloaded method. This method is allowed to // use the global image number variable for both / input and output. In other words, the method / should check if image number +1 is less thar // or equal to MAX NUMBER. If it is, then the // method increases image number by 1. Othwerise, // the method sets image number to MIN NUMBER // This method should “wrap aro / forward ) method does und” like the first public static void backward //, OverLoaded method. This method is allowed to // use the global image number variable for both // input and output. In other words, the method // should check if image number 1 is greater // than or equal to MIN NUMBER. If it is, then // the method decreases image number by 1 // Otherwise, the method sets image number to MIN NUMBER. Like the first backward) method, / this method should NOT “wrap around” arameter), these NEW In addition to having different parameters (no parameter at all versus one p method overloads will operate differently. Instead of using a parameter for input and a returned value output, these new method overloads are allowed to use the global image number variable. Add two new options to your program’s menu to invoke these new, overloaded methods. Both the forward) and backward() method overloads should do basically the same thing as the original forward) and backward() methods, but these overloaded versions do NOT require a parameter and do not return anything. Test your program. Make sure you can page forward using both forward() methods and backward using both backward() methods. DO NOT CONTINUE TO PHASE 4 UNTIL YOU ARE SURE YOUR PROGRAM WORKS AS INTENDED. Phase 4 (6 points) im es d some code to invoke the existing showwindow) method, pass the filenames the createFileNamel) and createRandomNamel) methods create in your showMenu() method’s switch, under the case which invokes your createFileName) method, add a statement which invokes the showWindow) method, passing the String returned by createFileName() as the argument. Also in your showMenu() method’s switch, under the case which invokes your createRandomName() method, add a statement which invokes the showwindowl) method, passing the String returned by createRandomName) as the argument Test your program. You should be able to page forward and backward with proper behavior when limits are reached. When either createFileNamel) or createRandomName( are invoked, a taskbar to see frame containing an i mage should appear onscreen (you The correct filename should be shown at the top of the frame. In other words, if the may need to click a button the it). createFileName() method is invoked when image number is 7, a frame will open and the frame should show a picture and the title bar of the frame should be picture7.jpg. If the createRandomName() method is invoked, a frame will open and the from should show a picture and the title bar of the frame should be a random filename between and including picture1.jpg to picture8.jpg. All 8 pictures in this project are different from one another. Remember to write a Universal comment header at the top of your source code file. When you have completed the exercise, ZIP the entire project folder and upload this ZIP file to Canvas In NetBeans, click File, select Export Project, click To ZIP.., select the location where you want the ZIP file to be saved, type the name of the ZIP file, Project4.zip in the textbox, click the Save button, then click the Export button ve to manually ZIP the If the IDE you are using does not have an export-project-to-zip option, you may h file by navigating to the project folder in a file browser and selecting your oS’s “compress folder Under Mac OS, the file browser is called Finder which offers a Compress Folder option. Under Windows, the file browser is called File Explorer and it offers a Send to > option. Compressed (zipped) folder Show transcribed image text Phase 1 (3 points) Design your methods in this phase, we will revisit the concept of an algorithm. Remember, an algorithm is a detailed set of instructions which describe how you intend to solve a specific problem in a finite amount of time Therefore, to complete this phase, you must design 3 algorithms. Write out both a flow chart and pseudocode for the algorithms, then show these algorithms to me in lab class. Here is a description of each algorithm: Given an integer-type parameter named current number, a global integer variable named image number, and two global symbolic constants: final static int MIN NUMBER 1: final static int MAX NUMBER 8: static int image-number = 1; Write an algorithm named forward which increases the value passed to its parameter current number by one each time the algorithm is invoked. In other words, when forward is invoked and passed a number like 1, then forward returns a value of 2. When forward is invoked and passed a value of 2, then forward returns a value of 3. Hówever, when MAX NUMBER is reached, value larger than MAX NUMBER. Write an algorithm named backward which current_number by one each time the algorithm is invoked. In other words, when backward is invoked and passed a number like 8, then backward returns a value of 7. When backward and passed a number like 7, then backward returns a value of 6. However, when MIN _NUMBER is reached, the algorithm should STOP and return the value of MIN NUMBER. This algorithm will NEVER “wrap around” decreases the value passed to its parameter is invoked Write an algorithm named createFileName which concatenates and returns a String like createFileName is invoked and passed a number like 8, then createFileName returns the String picture8.jpg. When createFileName is invoked and passed a number like 1, then createFileName returns the String picture1.jpg. pictureX.jpg where X is the value of the input parameter current_number. In other words, when BE SURE TO HAV EME CHECKANDAPPROVEYOURALGORİTHMSBEFOREPROCEEDINGTO PHASE 2. NO ADDITIONAL POINTS WILL BE AWARDED IF YOU DO NOT SHOW ME YOUR ALGORITHMS
Phase 2 (12 points). Implement your algorithms in this phase, you will im attached PictureViewer class your own methods to this class. Your methods should behave as described in your that is, the forward()method should “wrap around” when it reaches plement your algorithms in an actual Java class by adding six methods to the Do not modify the existing code in the PictureViewer class, simply add Phase 1 aigorithms, it reaches the last number, the kward() method should STOP when it reaches the first number, and the createFi1elame) method should return the String picture8.jpg when the method is invoked and passed a number like B Your forward () and backward () methods MUST use a parameter named current number for input and MUST return an int-type value, they MUST NOT use the global image_number variable directly. Also, both methods SHOULD use the symbolic constants MIN NUMBER and MAX NUMBEP instead of the hardcoded, literal numbers 1 and 8. Don’t try to make the methods overly complex, they should be very simple; when forward 0 is passed a current_number of 3, the method returns a 4. When backwardl) is passed a current number of 6, the method returns a 5. The createFileName() method will use a parameter named current _number for input, and will return a String representing a filename like pictureX.jpg, where X is the value of current number. Use the following method headers: public static int forward(int current number) public static int backward(int current _number) // The static methods above should use the symbolic constants // return the new number // return the new number MIN NUMBER, MAX NUMBER. Do not use hardcoded 1 or 8 public static string createFileName (int current number) // return a String representing a filename like picturex.jpg public static String createRandomName () f // Return a String representing a filename 1ike picturex.jpg // where X is a random number between MIN NUMBER and MAX NUMBER
ctrl public static void showMenu () //write a loop 1. Inside the loop, display a menu with options 1..N for each of the each of the methods above as well as an exit option 2. Print the value of the global image number variable 3. Prompt the user for a menu option choice 4. Get the user’s menu option choice, then invoke the correct method using a SWITCH. For example, if your menu shows option 1. Forward, and the user’s menu option choice is 1, the switch should then invoke the forward) method public static void main (Stringl) args) // Invoke showMenu () here // Learning how to invoke a method is crucial to this chapter, // so DO NOT ask a fellow student or even the instructor how // to do this pictureX.jpg, where X is a random number between MIN NUMBER and MAX NUMBER, inclusive The menu displayed by showMenu() should have options for invoking forward/). backward). global image n to use ) and createFileNamel) methods. to update image_number by assigning it the numbers returned by the forward) or ) method. Also, when the exit option is chosen in showMenu(), use System.exit (0) to terminate the program. At this point, your program should display a menu, invoke methods and print the image number in the NetBeans console output window Phase 3 (9 points) overloaded methods. An overloaded method is one that has We can create a method with the same name as another, as long This phase will require you to write two the SAME NAME as another method. as it has a different list of parameters. The difference must be in either the number of the data types of those parameters. Add the following OVERLOADED methods to your PictureViewer class: parameters or in
e static void forward() pub overloaded method. This method is allowed to // use the global image number variable for both / input and output. In other words, the method / should check if image number +1 is less thar // or equal to MAX NUMBER. If it is, then the // method increases image number by 1. Othwerise, // the method sets image number to MIN NUMBER // This method should “wrap aro / forward ) method does und” like the first public static void backward //, OverLoaded method. This method is allowed to // use the global image number variable for both // input and output. In other words, the method // should check if image number 1 is greater // than or equal to MIN NUMBER. If it is, then // the method decreases image number by 1 // Otherwise, the method sets image number to MIN NUMBER. Like the first backward) method, / this method should NOT “wrap around” arameter), these NEW In addition to having different parameters (no parameter at all versus one p method overloads will operate differently. Instead of using a parameter for input and a returned value output, these new method overloads are allowed to use the global image number variable. Add two new options to your program’s menu to invoke these new, overloaded methods. Both the forward) and backward() method overloads should do basically the same thing as the original forward) and backward() methods, but these overloaded versions do NOT require a parameter and do not return anything. Test your program. Make sure you can page forward using both forward() methods and backward using both backward() methods. DO NOT CONTINUE TO PHASE 4 UNTIL YOU ARE SURE YOUR PROGRAM WORKS AS INTENDED.
Phase 4 (6 points) im es d some code to invoke the existing showwindow) method, pass the filenames the createFileNamel) and createRandomNamel) methods create in your showMenu() method’s switch, under the case which invokes your createFileName) method, add a statement which invokes the showWindow) method, passing the String returned by createFileName() as the argument. Also in your showMenu() method’s switch, under the case which invokes your createRandomName() method, add a statement which invokes the showwindowl) method, passing the String returned by createRandomName) as the argument Test your program. You should be able to page forward and backward with proper behavior when limits are reached. When either createFileNamel) or createRandomName( are invoked, a taskbar to see frame containing an i mage should appear onscreen (you The correct filename should be shown at the top of the frame. In other words, if the may need to click a button the it). createFileName() method is invoked when image number is 7, a frame will open and the frame should show a picture and the title bar of the frame should be picture7.jpg. If the createRandomName() method is invoked, a frame will open and the from should show a picture and the title bar of the frame should be a random filename between and including picture1.jpg to picture8.jpg. All 8 pictures in this project are different from one another. Remember to write a Universal comment header at the top of your source code file. When you have completed the exercise, ZIP the entire project folder and upload this ZIP file to Canvas In NetBeans, click File, select Export Project, click To ZIP.., select the location where you want the ZIP file to be saved, type the name of the ZIP file, Project4.zip in the textbox, click the Save button, then click the Export button ve to manually ZIP the If the IDE you are using does not have an export-project-to-zip option, you may h file by navigating to the project folder in a file browser and selecting your oS’s “compress folder Under Mac OS, the file browser is called Finder which offers a Compress Folder option. Under Windows, the file browser is called File Explorer and it offers a Send to > option. Compressed (zipped) folder

Expert Answer


Answer to I got my algorithms down but i cant seem to implement them into the code right. When i try to invoke the forward or back…

OR