Menu

[solved]-Hello Following Code Done Need Help Phase 3 Phase 4 Would Really Appreciate Help Phase 3 A Q39038788

Hello I have the following code done. I need help withPHASE 3 and PHASE 4. Would really appreciate thehelp..

Phase 3 – Adding Overloaded Methods
This phase will require you to write two overloaded methods. Anoverloaded method is one that has the same name as another method.We can create a method with the same name as another, as long as ithas a different list of parameters. The difference must be ineither the number of parameters or in the data types of thoseparameters. Add the following overloaded methods to thePictureViewer class:
public static void forward(){
// Overloaded method. This method is allowed to
// use the global variable image_number for
// input and output. In other words, the method
// should check if image_number + 1 is less than
// or equal to MAX_NUMBER. If it is, then the
// metho sets image_number to image_number + 1.
// Otherwise, the method sets image_number to
// MIN_NUMBER.
}
public static void backward(){
// Overloaded method. This method is allowed to
// use the global variable image_number for
// 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 sets image_number to image_number – 1.
// Othecrwise, the method sets image_number to
// MIN_NUMBER.
}
In addition to having different parameters (no parameter at allversus one parameter), these new methods will operate differently.Instead of using a parameter and return data type for input andoutput, these new methods will be allowed to use the image_numberglobal variable for input and output.
Add two new options to your mentu to invoke these new overloadedmethods. Both versions of forward() and backward() do basically thesame thing as the previous forward() and backward() methods, butthese overloaded versions do not require a parameter. Test yourprogram. Make sure you can move forward using both overloadedforward() methods and backward using both overloaded backward()methods. Do not continue to Phase 4 until you are sure the programworks.

Phase 4 – Showing Images
Add some code to invoke the existing showWindow() method, anddisplay the image file names the Phase 2 and 3 methodscreate.
In your showMenu() method’s if-statement, where you invoke thecreateFileName() method, add a statement to invoke showWindow()then pass showWindow() the file name returned by createFileName().Where you invoke the createRandomName() method, add a statement toinvoke showWindow() then pass showWindow() the file name returnedby the createRandomName() method.
Test your program. You should be able to go forward and backwardwith proper behavior when limits are reached. When eithercreateFileName() or createRandomName() methods are invoked, a framcontaining an image should appear onscreen (you may need to click abutton on the taskbar to see it). The current file name should beshown at the top of the frame. In other words, if thecreateFileName() method is invoked when image_number is 7, a framwill open and the fram should show a picture and the title bar ofthe fram should show “picture7.jpg”. If the createRandomName()method is invoked, a frame will open and the frame should show apicture and the title bar of the frame should show a random filename between and including “picture1.jpg” to “picture8.jpg”. All 8pictures in this project are different from one another.
Remember to write a Universal comment header at the top of yoursource code file.

*

import java.util.Random;
import java.util.Scanner;

public class Methods {
final static int MIN_NUMBER = 1;
final static int MAX_NUMBER = 8;
  
public static int forward(int current_number) {
if (current_number < MAX_NUMBER){
return current_number + 1;
}
else {
return MIN_NUMBER;
}
}
  
public static int backward(int current_number){
if (current_number > MIN_NUMBER){
return current_number – 1;
}
else{
return MIN_NUMBER;
}
}
  
public static String createFileName(int current_number){
return “picture” + current_number + “.jpg”;
}
  
public static String createRandomName(){
Random rand = new Random();
int generate_num = rand.nextInt(7);
return “picture” + generate_num + “jpg”;
}
  
public static void showMenu(){
Scanner userInput = new Scanner(System.in);
boolean opt = true;
int output = 0;
String outputFileName;
String loop = ” “;
int selOpt, current_number = 0;
do {
System.out.println(“Select an Option(1-4): “);
System.out.println(“1. Forward “);
System.out.println(“2. Backward “);
System.out.println(“3. CreateFileName “);
System.out.println(“4. CreateRandomName “);
selOpt = userInput.nextInt();
if (selOpt == 1 || selOpt == 2 || selOpt == 3){
System.out.println(“Enter current number: “);
current_number = userInput.nextInt();
}
switch(selOpt) {
case 1 : output = forward(current_number);
System.out.println(“Image number is ” + output);
break;
case 2: output = backward(current_number);
System.out.println(“Image number is ” + output);
break;
case 3: outputFileName = createFileName(current_number);
System.out.println(“Name of the image is ” + outputFileName);
break;
case 4: outputFileName = createRandomName();
System.out.println(“Name of the image is ” + outputFileName);
break;
}
System.out.println(“Do you want to continue?: (Y/N) “);
loop = userInput.next();
  
if (loop == “y” || loop == “Y”){
opt = true;
}
else {
opt = false;
}
  
}
while (opt);
}
public static void main(String[] args){
showMenu();
}
}

*

I’m supposed to call the methods to thiscode.

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

Expert Answer


Answer to Hello I have the following code done. I need help with PHASE 3 and PHASE 4. Would really appreciate the help.. Phase 3 -… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *