Question

When I invoke the forward or backward method the current_number isnt being changed. Not sure if...

When I invoke the forward or backward method the current_number isnt being changed. Not sure if its something in my switch case or my methods..

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 = 0;
final static int MAX_NUMBER = 8;
static int image_number = 1;
  

public static int forward(int current_number) {
if (current_number < MAX_NUMBER) {
return 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) {
return 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);
while (true) {
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");

int current_number = 0;
int user = input.nextInt();

switch (user) {
case 1:
System.out.println("static forward");
forward(current_number);   
  
break;
case 2:
System.out.println("static backward");
backward(current_number);

break;
case 3:
System.out.println("createFileName");
showWindow(createFileName(current_number));
break;
case 4:
System.out.println("createRandomName");
showWindow(createRandomName());
break;

}
}
}
public void forward() {

if (image_number < MAX_NUMBER) {
image_number++;
} else {
image_number = MAX_NUMBER;
}
}

public void backward() {

if (image_number > MIN_NUMBER) {
image_number--;
}
}

public static void main(String[] args) {

showMenu();
}

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The problem with your code is that though you are calling the forward and backward methods , you are not saving the state of current number after the function call. To tell you in simple terms, store the return value of those functions.

So, replace your function calls as below.

case 1:
System.out.println("static forward");
current_number = forward(current_number);   
  
break;
case 2:
System.out.println("static backward");
current_number = backward(current_number);

break;

Add a comment
Know the answer?
Add Answer to:
When I invoke the forward or backward method the current_number isnt being changed. Not sure if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with programming in C++ Phase 1 is complete, please help with phase 2. Thank you....

    Help with programming in C++ Phase 1 is complete, please help with phase 2. Thank you. Phase 1 You must 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: const int MIN_NUMBER = 1; const 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...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • Can you fix my error? I created a program that changes labels every second in Java....

    Can you fix my error? I created a program that changes labels every second in Java. But, it does not change. And, it will starts with second label. This is also an error. import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Map2 extends JFrame{ JPanel panel; JLabel pic; Timer tm; int x = 0; String ly = "<html> " + "<br> <font size='10' color='red'> <b> 111111111 <b/> </font>...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT