Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with Part III, Please explain in detail. thank you Part II Implement your algorithms with three methods for this phase. The methods

I need help with Part III, Please explain in detail.

thank you

Part II

Implement your algorithms with three methods for this phase. The methods should behave as described in phase 1. The forward method should wrap around when it reaches the last number, the backward method should stop when it reaches the first number.

Add the MIN_NUMBER and MAX_NUMBER constants at the top of your class, you will also need to add a global variable at the top of your class:

final static int MIN_NUMBER = 1;

final static int MAX_NUMBER = 8;

static int image_number = 1;

The forward() and backward() methods MUST use an input parameter and output a return value, they DO NOT use the above global variable image_number directly, but they should use the constants . The methods are very simple, when the current image number is 3 forward changes it to a 4 and returns it. When the current image number is 6 backward() changes it to a 5 and returns it. The method createFileName() will take a number as input, and returns a String containing a file name like "pictureX.gif". The method createRandomName() has no input, and returns a String containing a file name like "pictureX.gif".

Implement the following methods

public static int forward ( int current_number ) {

// return the new image number

}

public static int backward ( int current_number ) {

// return the new image number

}

// use the constants MIN_NUMBER, MAX_NUMBER, do not use hard coded 1 or 8

public static String createFileName ( int current_number ) {

// return a filename like pictureX.gif

}

public static String createRandomName ( ) {

// return a filename like pictureX.gif

// using a RANDOM number between MIN_NUMBER and MAX_NUMBER

}

public static void showMenu ( ) {

// write a loop

// Display a menu, with options 1 .. N for each method above, and an exit option

// get user input and call the correct method using a SWITCH

// print out the NEW image number everytime the value changes

}

public static void main ( String [] args) {

// call showMenu

}

The menu should have options for calling forward(), backward(), createFileName () and createRandomName ( ). Be sure to use a parameter when calling the functions, then use the return value to update the global image number. At this point, your program will show the menu, call the methods and print onto the console the new image number everytime it changes.

Show transcribed image text

?? ?import java.util.Random;

import java.util.Scanner;?public class ques1

final static int MIN_NUMBER=1;

final static int MAX_NUMBER=8;

public static int forward(int current_number)

if(current_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+".gif";

public static String createRandomName()

Random rand = new Random();

int generate_number = rand.nextInt(7);

return "picture"+generate_number+".gif";

public static void showMenu()

boolean opt=true;

int result=0;

String resultFilename;

String loopcont=" ";

int selOpt,current_number=0;

do

{

System.out.println("Please Select an Option(Input an integer value");

System.out.println("1.Forward");

System.out.println("2.Backward");

System.out.println("3.CreateFileName");

System.out.println("4.CreateRandomName");

Scanner sc=new Scanner(System.in);

selOpt=sc.nextInt();

if(selOpt==1 || selOpt==2 || selOpt==3)

{

System.out.println("Please Input current Number");

current_number=sc.nextInt();

}

switch(selOpt)

{

case 1:result=forward(current_number);

System.out.println("Image No is "+result);

break;

case 2:result=backward(current_number);

System.out.println("Image No is "+result);

break;

case 3:resultFilename=createFileName(current_number);

System.out.println("Name of the Image is "+resultFilename);

break;

case 4:resultFilename=createRandomName();

System.out.println("Name of the Image is "+resultFilename);

break;

default:System.out.println("No More Options");

}

System.out.println("Do you want to continue:Y/N");

loopcont=sc.next();

if ( loopcont.equals("y") || loopcont.equals("Y") || loopcont.equals("Yes") || loopcont.equals("yes"))

{

opt=true;

}

else

opt=false;

public static void main(String args[])

showMenu();

}??

Part III

Static vs. Non-Static Methods

A static method does NOT need an object to execute. A non-static method REQUIRES an object to execute. Creating an object is simple, inside of the showMenu method:

ClassName theobject = new ClassName(); // e.g. public class ClassName { ....

With this variable you can now call non-static methods using the dot '.' operator:

theobject.method(); // e.g. call a method that is NON-static using an object

Now we need to create some non-static methods! Overloaded methods are two methods with the SAME name. We can create a method with the same name as long as it has a different number or type of parameters. Add the following OVERLOADED methods to your program:

public void forward ( ) { // overloaded method, use global variable as input and output } public void backward ( ) { // overloaded method, use global variable as input and output }

In addition to having different parameters, these NEW functions will operate differently. Instead of using parameter and return type for input and output, these will be allowed to use the image number global variable for input and output. Add two NEW options to your menu to call these new methods. Both versions of forward() and backward() do basically the same thing, but the non-static versions require an object to execute. Test your program, make sure you can move forward and backward using both techniques. Do not continue to phase 4 until you are sure the program works.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Factory Active Database For Enterprise Computing

Authors: Schur, Stephen

1st Edition

0471558443, 9780471558446

More Books

Students also viewed these Databases questions