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[]...

  • Why when executed my frame does not contain what i have added to it? Here is...

    Why when executed my frame does not contain what i have added to it? Here is my code import java.awt.Panel; 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.JTextArea; import javax.swing.JTextField; import javax.swing.JPanel; @SuppressWarnings("serial") public class AddressBook1 extends JFrame implements ActionListener {    /**    *    */       JLabel name = new JLabel("Name: ");    JLabel address = new JLabel("Address: ");    JLabel phone = new JLabel("Phone: ");    JLabel email = new JLabel("Email: ");...

  • I got this programming its used to highlight text, but i couldn't remove highlight again from...

    I got this programming its used to highlight text, but i couldn't remove highlight again from the text. i need someone to write me code that removes text highlight here is my code import java.awt.*; import java.awt.event.*; import java.util.Map; import java.util.HashMap; import javax.swing.*; import javax.swing.text.*; public class TextHighlight {    private JTextArea textarea;    private JComboBox colorbox;    private JTextField top;    private String[] colorOptions = { "GRAY", "YELLOW", "RED", "CYAN", "ORANGE", "PINK" };    private Highlighter.HighlightPainter grayPainter;    private...

  • 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...

  • package timing; import java.awt.event.*; import javax.swing.*; public class Timing implements ActionListener{     JButton button1;     JLabel...

    package timing; import java.awt.event.*; import javax.swing.*; public class Timing implements ActionListener{     JButton button1;     JLabel label1;     public int minutes=3;//initial time     public int time=minutes*60;//total amount of time in seconds only     public int seconds=time%60;     public String defaulttimeset="0"+Integer.toString(minutes)+":0"+Integer.toString(seconds);     public static void main(String[] args) {         Timing move=new Timing();         move.go();     }     public void go(){         JFrame frame=new JFrame();         frame.setBounds(50,50,400,400);         JPanel panel2=new JPanel();         button1=new JButton("Start Time");         panel2.add(button1);                 button1.setBounds(30,50,150,170);         frame.getContentPane().add(button1);...

  • 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...

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