Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the default case, also indicate whether the input value is greater than 4 or

ID: 3718649 • Letter: F

Question

For the default case, also indicate whether the input value is greater than 4 or less than 0 and whether the value is even or odd.
Be sure to test with each of the valid input values suggested in the textbook: the integers from 0 to 4 (0, 1, 2, 3, and 4) plus some other invalid integer values, such as 5, -1, 10, and -6.
The following is an example of the console output from one testing session:
0 was entered. 1 was entered. 2 was entered. 3 was entered. 4 was entered. The invalid value 5 was entered, which is greater than 4 and odd. The invalid value -1 was entered, which is less than 0 and odd. The invalid value 10 was entered, which is greater than 4 and even. The invalid value -6 was entered, which is less than 0 and even.
Programming Activity 5-2 Guidance =================================
Input parameter --------------- The parameter "value" is passed into the function where you put your code. Therefore, it is available for you to use in your code.
Switch statement ---------------- You must use a switch statement that switches on the input parameter "value".
Switch statement default case ----------------------------- Within the switch statement default case, use nested "if/else" statements to produce the correct console output.
Even integer ------------ To determine if an integer is even, the condition to test is (value % 2 == 0).
Animation calls --------------- The final statement of each specific value case must be: animate(#, value); where you replace # by the actual value of the case. For example: animate(3, value); The final statement of your default case must be: animate(-1, value); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* MultiPath * Anderson, Franceschi */
import java.awt.Graphics; import java.awt.Color; import java.awt.Font;
public class MultiPath { // array path keeps track of the path private boolean [] path = { false, false, false, false, false, false };
// count counts how many times the method animate was called // therefore deducting when the code hits the first break statement private int count = 0;
// current is the value of test in MultiPathClient private int current;
// makes sure we only draw once private boolean control = true;
// 2 seconds of pause between the 2 images private static int animationPause = 2;
public MultiPath( ) { }
public MultiPath( boolean [] p, int c1, int c2 ) { setPath( p ); count = c1; current = c2; }
public void resetPath( ) { for ( int i = 0; i < path.length; i++ ) { path[i] = false; } }
public void setPath( boolean [] p ) { for ( int i = 0; i < path.length; i++ ) { path[i] = p[i]; } }
public void setPath( int i, boolean b ) { if ( i >= 0 && i < path.length ) path[i] = true; else path[path.length - 1] = true; }
public void setCount( int c ) { count = c; }
public int getCount( ) { return count; }
public void setCurrent( int c ) { current = c; }
public void setControl( boolean b ) { control = b; }
public void addToCount( ) { count ++; }
public int firstIndexTrue( ) { int temp = -1; for ( int i = 0; i < path.length; i++ ) { if ( path[i] ) { return i; } } return temp; }
public int lastIndexTrue( ) { return ( firstIndexTrue( ) + count - 1 ); }
public void displayCountAndIndexes( ) { int firstIndex = firstIndexTrue( ); System.out.print( "count = " + count ); System.out.print( " firstIndex = " + firstIndex ); System.out.println( " lastIndex true = " + ( firstIndex + count - 1 ) ); }
public void draw( Graphics g ) { // displayCountAndIndexes( ); if ( control ) drawPath( g ); }
public void drawPath( Graphics g ) { int startX = 100, startY = 25; g.setColor( new Color( 127,255,212 ) ); // vertical entry line (1st line) // g.drawLine(startX, startY, startX, startY + 25); g.fillRect( startX - 2, startY, 5, 25 ); // entry rectangle (1st rectangle) g.fillRoundRect( startX - 50, startY + 25, 100, 40, 10, 10 ); // 2nd vertical line // g.drawLine(startX, startY + 65, startX, startY + 90); g.fillRect( startX - 2, startY + 65, 5, 25 ); // 2nd rectangle g.fillRoundRect( startX - 50, startY + 90, 100, 40, 10, 10); // 8th right line g.drawLine( startX + 250, startY + 430, startX + 250, startY + 480 ); // last rectangle g.fillRoundRect( startX + 200, startY + 480, 100, 40, 10, 10 ); // last line // g.drawLine(startX + 250, startY + 520, startX + 250, startY + 545); g.fillRect( startX + 248, startY + 520, 5, 25 ); ////////////////////////////////////////// // NOW select drawing color depending on objects to draw and path
// left rectangles g.setColor( Color.LIGHT_GRAY ); for ( int i = 1; i < path.length; i++ ) { if ( i <= firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect(startX - 50, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// left vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i < firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX - 2, startY + 130 + (65 * i), 5, 25); }
// middle vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i >= firstIndexTrue( ) && i < lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor(Color.lightGray); g.fillRect(startX + 148, startY + 130 + (65 * i), 5, 25); }
// right vertical lines g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 248, startY + 110 + (65 * i), 5, 65 ); }
// left horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 50, startY + 110 + (65 * i), 50, 5 ); }
// 2nd right rectangles g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= firstIndexTrue( ) && i <= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect( startX + 100, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// right horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 200, startY + 110 + (65 * i), 48,5 ); }
g.setColor( Color.BLACK ); g.drawString( "switch(test)", startX - 35, startY + 50 ); g.drawString( "case(0) ?", startX - 35, startY + 115 ); g.drawString( "case(1) ?", startX - 35, startY + 180 ); g.drawString( "case(2) ?", startX - 35, startY + 245 ); g.drawString( "case(3) ?", startX - 35, startY + 310 ); g.drawString( "case(4) ?", startX - 35, startY + 375 ); g.drawString( "default", startX - 35, startY + 440 );
g.drawString( "Value is 0", startX + 115, startY + 115 ); g.drawString( "Value is 1", startX + 115, startY + 180 ); g.drawString( "Value is 2", startX + 115, startY + 245 ); g.drawString( "Value is 3", startX + 115, startY + 310 ); g.drawString( "Value is 4", startX + 115, startY + 375 ); g.drawString( "Not a valid value", startX + 105, startY + 440 );
g.drawString( "Continue", startX + 225, startY + 505 );
g.setFont( new Font( "Serif", Font.BOLD, 18 ) ); g.setColor( Color.RED ); g.drawString( "Value of test is " + current, startX + 110, startY + 50 );
} } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* MultiPathClient.java Anderson, Franceschi */
import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JOptionPane;
public class MultiPathClient extends JFrame { private final int SIZE = 6;
public static boolean [] array; private static int count1 = 0; private static int current1 = -1;
private boolean firstTime = true;
private int key; private MultiPath mp;
public MultiPathClient() { super("Illustrating switch .. case");
array = new boolean[SIZE];
// fill with values false for (int i = 0; i < array.length; i++) { array[i] = false; }
mp = new MultiPath(array, count1, current1);
setSize(500, 600); setVisible(true); }
// ***** Student writes this method public void workWithSwitch(int value) { // Student code starts here:


// Student code ends here.
mp.setControl(false); mp.resetPath(); mp.setCount(0); mp.setCurrent(-1); }
public void startActivity() { boolean goodInput = false; while (!goodInput) { try { String answer = JOptionPane.showInputDialog(null, "Enter an integer");
if (answer != null) { key = Integer.parseInt(answer); goodInput = true; } else { System.exit(0); } } catch(Exception e) {} } if (goodInput) { firstTime = false; mp.setControl(true); workWithSwitch(key); } }
public static int getCount1() { return count1; }
public static int getCurrent1() { return current1; }
public static boolean [] getArray() { return array; }
private void animate(int index, int value) { try { mp.setCurrent(value); mp.setPath(index, true); mp.addToCount(); repaint(); Thread.sleep(100); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } }
public void paint(Graphics g) { super.paint(g); if (!firstTime) { mp.draw(g); } }
public static void main(String [] args) { MultiPathClient app = new MultiPathClient(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); PrintArrayT t = new PrintArrayT(app); t.start(); }
} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* PrintArrayT * Anderson, Franceschi */
public class PrintArrayT extends Thread { boolean [] arr; MultiPathClient mp1;
public PrintArrayT( MultiPathClient m ) { arr = MultiPathClient.array; mp1 = m; }
public void run( ) { while( true ) { try { Thread.sleep( 500 ); } catch( Exception e ) {} mp1.startActivity( ); } } } For the default case, also indicate whether the input value is greater than 4 or less than 0 and whether the value is even or odd.
Be sure to test with each of the valid input values suggested in the textbook: the integers from 0 to 4 (0, 1, 2, 3, and 4) plus some other invalid integer values, such as 5, -1, 10, and -6.
The following is an example of the console output from one testing session:
0 was entered. 1 was entered. 2 was entered. 3 was entered. 4 was entered. The invalid value 5 was entered, which is greater than 4 and odd. The invalid value -1 was entered, which is less than 0 and odd. The invalid value 10 was entered, which is greater than 4 and even. The invalid value -6 was entered, which is less than 0 and even.
Programming Activity 5-2 Guidance =================================
Input parameter --------------- The parameter "value" is passed into the function where you put your code. Therefore, it is available for you to use in your code.
Switch statement ---------------- You must use a switch statement that switches on the input parameter "value".
Switch statement default case ----------------------------- Within the switch statement default case, use nested "if/else" statements to produce the correct console output.
Even integer ------------ To determine if an integer is even, the condition to test is (value % 2 == 0).
Animation calls --------------- The final statement of each specific value case must be: animate(#, value); where you replace # by the actual value of the case. For example: animate(3, value); The final statement of your default case must be: animate(-1, value); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* MultiPath * Anderson, Franceschi */
import java.awt.Graphics; import java.awt.Color; import java.awt.Font;
public class MultiPath { // array path keeps track of the path private boolean [] path = { false, false, false, false, false, false };
// count counts how many times the method animate was called // therefore deducting when the code hits the first break statement private int count = 0;
// current is the value of test in MultiPathClient private int current;
// makes sure we only draw once private boolean control = true;
// 2 seconds of pause between the 2 images private static int animationPause = 2;
public MultiPath( ) { }
public MultiPath( boolean [] p, int c1, int c2 ) { setPath( p ); count = c1; current = c2; }
public void resetPath( ) { for ( int i = 0; i < path.length; i++ ) { path[i] = false; } }
public void setPath( boolean [] p ) { for ( int i = 0; i < path.length; i++ ) { path[i] = p[i]; } }
public void setPath( int i, boolean b ) { if ( i >= 0 && i < path.length ) path[i] = true; else path[path.length - 1] = true; }
public void setCount( int c ) { count = c; }
public int getCount( ) { return count; }
public void setCurrent( int c ) { current = c; }
public void setControl( boolean b ) { control = b; }
public void addToCount( ) { count ++; }
public int firstIndexTrue( ) { int temp = -1; for ( int i = 0; i < path.length; i++ ) { if ( path[i] ) { return i; } } return temp; }
public int lastIndexTrue( ) { return ( firstIndexTrue( ) + count - 1 ); }
public void displayCountAndIndexes( ) { int firstIndex = firstIndexTrue( ); System.out.print( "count = " + count ); System.out.print( " firstIndex = " + firstIndex ); System.out.println( " lastIndex true = " + ( firstIndex + count - 1 ) ); }
public void draw( Graphics g ) { // displayCountAndIndexes( ); if ( control ) drawPath( g ); }
public void drawPath( Graphics g ) { int startX = 100, startY = 25; g.setColor( new Color( 127,255,212 ) ); // vertical entry line (1st line) // g.drawLine(startX, startY, startX, startY + 25); g.fillRect( startX - 2, startY, 5, 25 ); // entry rectangle (1st rectangle) g.fillRoundRect( startX - 50, startY + 25, 100, 40, 10, 10 ); // 2nd vertical line // g.drawLine(startX, startY + 65, startX, startY + 90); g.fillRect( startX - 2, startY + 65, 5, 25 ); // 2nd rectangle g.fillRoundRect( startX - 50, startY + 90, 100, 40, 10, 10); // 8th right line g.drawLine( startX + 250, startY + 430, startX + 250, startY + 480 ); // last rectangle g.fillRoundRect( startX + 200, startY + 480, 100, 40, 10, 10 ); // last line // g.drawLine(startX + 250, startY + 520, startX + 250, startY + 545); g.fillRect( startX + 248, startY + 520, 5, 25 ); ////////////////////////////////////////// // NOW select drawing color depending on objects to draw and path
// left rectangles g.setColor( Color.LIGHT_GRAY ); for ( int i = 1; i < path.length; i++ ) { if ( i <= firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect(startX - 50, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// left vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i < firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX - 2, startY + 130 + (65 * i), 5, 25); }
// middle vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i >= firstIndexTrue( ) && i < lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor(Color.lightGray); g.fillRect(startX + 148, startY + 130 + (65 * i), 5, 25); }
// right vertical lines g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 248, startY + 110 + (65 * i), 5, 65 ); }
// left horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 50, startY + 110 + (65 * i), 50, 5 ); }
// 2nd right rectangles g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= firstIndexTrue( ) && i <= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect( startX + 100, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// right horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 200, startY + 110 + (65 * i), 48,5 ); }
g.setColor( Color.BLACK ); g.drawString( "switch(test)", startX - 35, startY + 50 ); g.drawString( "case(0) ?", startX - 35, startY + 115 ); g.drawString( "case(1) ?", startX - 35, startY + 180 ); g.drawString( "case(2) ?", startX - 35, startY + 245 ); g.drawString( "case(3) ?", startX - 35, startY + 310 ); g.drawString( "case(4) ?", startX - 35, startY + 375 ); g.drawString( "default", startX - 35, startY + 440 );
g.drawString( "Value is 0", startX + 115, startY + 115 ); g.drawString( "Value is 1", startX + 115, startY + 180 ); g.drawString( "Value is 2", startX + 115, startY + 245 ); g.drawString( "Value is 3", startX + 115, startY + 310 ); g.drawString( "Value is 4", startX + 115, startY + 375 ); g.drawString( "Not a valid value", startX + 105, startY + 440 );
g.drawString( "Continue", startX + 225, startY + 505 );
g.setFont( new Font( "Serif", Font.BOLD, 18 ) ); g.setColor( Color.RED ); g.drawString( "Value of test is " + current, startX + 110, startY + 50 );
} } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* MultiPathClient.java Anderson, Franceschi */
import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JOptionPane;
public class MultiPathClient extends JFrame { private final int SIZE = 6;
public static boolean [] array; private static int count1 = 0; private static int current1 = -1;
private boolean firstTime = true;
private int key; private MultiPath mp;
public MultiPathClient() { super("Illustrating switch .. case");
array = new boolean[SIZE];
// fill with values false for (int i = 0; i < array.length; i++) { array[i] = false; }
mp = new MultiPath(array, count1, current1);
setSize(500, 600); setVisible(true); }
// ***** Student writes this method public void workWithSwitch(int value) { // Student code starts here:


// Student code ends here.
mp.setControl(false); mp.resetPath(); mp.setCount(0); mp.setCurrent(-1); }
public void startActivity() { boolean goodInput = false; while (!goodInput) { try { String answer = JOptionPane.showInputDialog(null, "Enter an integer");
if (answer != null) { key = Integer.parseInt(answer); goodInput = true; } else { System.exit(0); } } catch(Exception e) {} } if (goodInput) { firstTime = false; mp.setControl(true); workWithSwitch(key); } }
public static int getCount1() { return count1; }
public static int getCurrent1() { return current1; }
public static boolean [] getArray() { return array; }
private void animate(int index, int value) { try { mp.setCurrent(value); mp.setPath(index, true); mp.addToCount(); repaint(); Thread.sleep(100); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } }
public void paint(Graphics g) { super.paint(g); if (!firstTime) { mp.draw(g); } }
public static void main(String [] args) { MultiPathClient app = new MultiPathClient(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); PrintArrayT t = new PrintArrayT(app); t.start(); }
} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* PrintArrayT * Anderson, Franceschi */
public class PrintArrayT extends Thread { boolean [] arr; MultiPathClient mp1;
public PrintArrayT( MultiPathClient m ) { arr = MultiPathClient.array; mp1 = m; }
public void run( ) { while( true ) { try { Thread.sleep( 500 ); } catch( Exception e ) {} mp1.startActivity( ); } } } For the default case, also indicate whether the input value is greater than 4 or less than 0 and whether the value is even or odd.
Be sure to test with each of the valid input values suggested in the textbook: the integers from 0 to 4 (0, 1, 2, 3, and 4) plus some other invalid integer values, such as 5, -1, 10, and -6.
The following is an example of the console output from one testing session:
0 was entered. 1 was entered. 2 was entered. 3 was entered. 4 was entered. The invalid value 5 was entered, which is greater than 4 and odd. The invalid value -1 was entered, which is less than 0 and odd. The invalid value 10 was entered, which is greater than 4 and even. The invalid value -6 was entered, which is less than 0 and even.
Programming Activity 5-2 Guidance =================================
Input parameter --------------- The parameter "value" is passed into the function where you put your code. Therefore, it is available for you to use in your code.
Switch statement ---------------- You must use a switch statement that switches on the input parameter "value".
Switch statement default case ----------------------------- Within the switch statement default case, use nested "if/else" statements to produce the correct console output.
Even integer ------------ To determine if an integer is even, the condition to test is (value % 2 == 0).
Animation calls --------------- The final statement of each specific value case must be: animate(#, value); where you replace # by the actual value of the case. For example: animate(3, value); The final statement of your default case must be: animate(-1, value); Programming Activity 5-2 Guidance =================================
Input parameter --------------- The parameter "value" is passed into the function where you put your code. Therefore, it is available for you to use in your code.
Switch statement ---------------- You must use a switch statement that switches on the input parameter "value".
Switch statement default case ----------------------------- Within the switch statement default case, use nested "if/else" statements to produce the correct console output.
Even integer ------------ To determine if an integer is even, the condition to test is (value % 2 == 0).
Animation calls --------------- The final statement of each specific value case must be: animate(#, value); where you replace # by the actual value of the case. For example: animate(3, value); The final statement of your default case must be: animate(-1, value); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* MultiPath * Anderson, Franceschi */
import java.awt.Graphics; import java.awt.Color; import java.awt.Font;
public class MultiPath { // array path keeps track of the path private boolean [] path = { false, false, false, false, false, false };
// count counts how many times the method animate was called // therefore deducting when the code hits the first break statement private int count = 0;
// current is the value of test in MultiPathClient private int current;
// makes sure we only draw once private boolean control = true;
// 2 seconds of pause between the 2 images private static int animationPause = 2;
public MultiPath( ) { }
public MultiPath( boolean [] p, int c1, int c2 ) { setPath( p ); count = c1; current = c2; }
public void resetPath( ) { for ( int i = 0; i < path.length; i++ ) { path[i] = false; } }
public void setPath( boolean [] p ) { for ( int i = 0; i < path.length; i++ ) { path[i] = p[i]; } }
public void setPath( int i, boolean b ) { if ( i >= 0 && i < path.length ) path[i] = true; else path[path.length - 1] = true; }
public void setCount( int c ) { count = c; }
public int getCount( ) { return count; }
public void setCurrent( int c ) { current = c; }
public void setControl( boolean b ) { control = b; }
public void addToCount( ) { count ++; }
public int firstIndexTrue( ) { int temp = -1; for ( int i = 0; i < path.length; i++ ) { if ( path[i] ) { return i; } } return temp; }
public int lastIndexTrue( ) { return ( firstIndexTrue( ) + count - 1 ); }
public void displayCountAndIndexes( ) { int firstIndex = firstIndexTrue( ); System.out.print( "count = " + count ); System.out.print( " firstIndex = " + firstIndex ); System.out.println( " lastIndex true = " + ( firstIndex + count - 1 ) ); }
public void draw( Graphics g ) { // displayCountAndIndexes( ); if ( control ) drawPath( g ); }
public void drawPath( Graphics g ) { int startX = 100, startY = 25; g.setColor( new Color( 127,255,212 ) ); // vertical entry line (1st line) // g.drawLine(startX, startY, startX, startY + 25); g.fillRect( startX - 2, startY, 5, 25 ); // entry rectangle (1st rectangle) g.fillRoundRect( startX - 50, startY + 25, 100, 40, 10, 10 ); // 2nd vertical line // g.drawLine(startX, startY + 65, startX, startY + 90); g.fillRect( startX - 2, startY + 65, 5, 25 ); // 2nd rectangle g.fillRoundRect( startX - 50, startY + 90, 100, 40, 10, 10); // 8th right line g.drawLine( startX + 250, startY + 430, startX + 250, startY + 480 ); // last rectangle g.fillRoundRect( startX + 200, startY + 480, 100, 40, 10, 10 ); // last line // g.drawLine(startX + 250, startY + 520, startX + 250, startY + 545); g.fillRect( startX + 248, startY + 520, 5, 25 ); ////////////////////////////////////////// // NOW select drawing color depending on objects to draw and path
// left rectangles g.setColor( Color.LIGHT_GRAY ); for ( int i = 1; i < path.length; i++ ) { if ( i <= firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect(startX - 50, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// left vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i < firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX - 2, startY + 130 + (65 * i), 5, 25); }
// middle vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i >= firstIndexTrue( ) && i < lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor(Color.lightGray); g.fillRect(startX + 148, startY + 130 + (65 * i), 5, 25); }
// right vertical lines g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 248, startY + 110 + (65 * i), 5, 65 ); }
// left horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 50, startY + 110 + (65 * i), 50, 5 ); }
// 2nd right rectangles g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= firstIndexTrue( ) && i <= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect( startX + 100, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// right horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 200, startY + 110 + (65 * i), 48,5 ); }
g.setColor( Color.BLACK ); g.drawString( "switch(test)", startX - 35, startY + 50 ); g.drawString( "case(0) ?", startX - 35, startY + 115 ); g.drawString( "case(1) ?", startX - 35, startY + 180 ); g.drawString( "case(2) ?", startX - 35, startY + 245 ); g.drawString( "case(3) ?", startX - 35, startY + 310 ); g.drawString( "case(4) ?", startX - 35, startY + 375 ); g.drawString( "default", startX - 35, startY + 440 );
g.drawString( "Value is 0", startX + 115, startY + 115 ); g.drawString( "Value is 1", startX + 115, startY + 180 ); g.drawString( "Value is 2", startX + 115, startY + 245 ); g.drawString( "Value is 3", startX + 115, startY + 310 ); g.drawString( "Value is 4", startX + 115, startY + 375 ); g.drawString( "Not a valid value", startX + 105, startY + 440 );
g.drawString( "Continue", startX + 225, startY + 505 );
g.setFont( new Font( "Serif", Font.BOLD, 18 ) ); g.setColor( Color.RED ); g.drawString( "Value of test is " + current, startX + 110, startY + 50 );
} } /* MultiPath * Anderson, Franceschi */
import java.awt.Graphics; import java.awt.Color; import java.awt.Font;
public class MultiPath { // array path keeps track of the path private boolean [] path = { false, false, false, false, false, false };
// count counts how many times the method animate was called // therefore deducting when the code hits the first break statement private int count = 0;
// current is the value of test in MultiPathClient private int current;
// makes sure we only draw once private boolean control = true;
// 2 seconds of pause between the 2 images private static int animationPause = 2;
public MultiPath( ) { }
public MultiPath( boolean [] p, int c1, int c2 ) { setPath( p ); count = c1; current = c2; }
public void resetPath( ) { for ( int i = 0; i < path.length; i++ ) { path[i] = false; } }
public void setPath( boolean [] p ) { for ( int i = 0; i < path.length; i++ ) { path[i] = p[i]; } }
public void setPath( int i, boolean b ) { if ( i >= 0 && i < path.length ) path[i] = true; else path[path.length - 1] = true; }
public void setCount( int c ) { count = c; }
public int getCount( ) { return count; }
public void setCurrent( int c ) { current = c; }
public void setControl( boolean b ) { control = b; }
public void addToCount( ) { count ++; }
public int firstIndexTrue( ) { int temp = -1; for ( int i = 0; i < path.length; i++ ) { if ( path[i] ) { return i; } } return temp; }
public int lastIndexTrue( ) { return ( firstIndexTrue( ) + count - 1 ); }
public void displayCountAndIndexes( ) { int firstIndex = firstIndexTrue( ); System.out.print( "count = " + count ); System.out.print( " firstIndex = " + firstIndex ); System.out.println( " lastIndex true = " + ( firstIndex + count - 1 ) ); }
public void draw( Graphics g ) { // displayCountAndIndexes( ); if ( control ) drawPath( g ); }
public void drawPath( Graphics g ) { int startX = 100, startY = 25; g.setColor( new Color( 127,255,212 ) ); // vertical entry line (1st line) // g.drawLine(startX, startY, startX, startY + 25); g.fillRect( startX - 2, startY, 5, 25 ); // entry rectangle (1st rectangle) g.fillRoundRect( startX - 50, startY + 25, 100, 40, 10, 10 ); // 2nd vertical line // g.drawLine(startX, startY + 65, startX, startY + 90); g.fillRect( startX - 2, startY + 65, 5, 25 ); // 2nd rectangle g.fillRoundRect( startX - 50, startY + 90, 100, 40, 10, 10); // 8th right line g.drawLine( startX + 250, startY + 430, startX + 250, startY + 480 ); // last rectangle g.fillRoundRect( startX + 200, startY + 480, 100, 40, 10, 10 ); // last line // g.drawLine(startX + 250, startY + 520, startX + 250, startY + 545); g.fillRect( startX + 248, startY + 520, 5, 25 ); ////////////////////////////////////////// // NOW select drawing color depending on objects to draw and path
// left rectangles g.setColor( Color.LIGHT_GRAY ); for ( int i = 1; i < path.length; i++ ) { if ( i <= firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect(startX - 50, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// left vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i < firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX - 2, startY + 130 + (65 * i), 5, 25); }
// middle vertical lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length - 1; i++ ) { if ( i >= firstIndexTrue( ) && i < lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor(Color.lightGray); g.fillRect(startX + 148, startY + 130 + (65 * i), 5, 25); }
// right vertical lines g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 248, startY + 110 + (65 * i), 5, 65 ); }
// left horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == firstIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 50, startY + 110 + (65 * i), 50, 5 ); }
// 2nd right rectangles g.setColor(Color.lightGray); for ( int i = 0; i < path.length; i++ ) { if ( i >= firstIndexTrue( ) && i <= lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRoundRect( startX + 100, startY + 90 + (65 * i), 100, 40, 10, 10 ); }
// right horizontal lines g.setColor( Color.LIGHT_GRAY ); for ( int i = 0; i < path.length; i++ ) { if ( i == lastIndexTrue( ) ) g.setColor( new Color( 127, 255, 212 ) ); else g.setColor( Color.LIGHT_GRAY ); g.fillRect( startX + 200, startY + 110 + (65 * i), 48,5 ); }
g.setColor( Color.BLACK ); g.drawString( "switch(test)", startX - 35, startY + 50 ); g.drawString( "case(0) ?", startX - 35, startY + 115 ); g.drawString( "case(1) ?", startX - 35, startY + 180 ); g.drawString( "case(2) ?", startX - 35, startY + 245 ); g.drawString( "case(3) ?", startX - 35, startY + 310 ); g.drawString( "case(4) ?", startX - 35, startY + 375 ); g.drawString( "default", startX - 35, startY + 440 );
g.drawString( "Value is 0", startX + 115, startY + 115 ); g.drawString( "Value is 1", startX + 115, startY + 180 ); g.drawString( "Value is 2", startX + 115, startY + 245 ); g.drawString( "Value is 3", startX + 115, startY + 310 ); g.drawString( "Value is 4", startX + 115, startY + 375 ); g.drawString( "Not a valid value", startX + 105, startY + 440 );
g.drawString( "Continue", startX + 225, startY + 505 );
g.setFont( new Font( "Serif", Font.BOLD, 18 ) ); g.setColor( Color.RED ); g.drawString( "Value of test is " + current, startX + 110, startY + 50 );
} } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* MultiPathClient.java Anderson, Franceschi */
import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JOptionPane;
public class MultiPathClient extends JFrame { private final int SIZE = 6;
public static boolean [] array; private static int count1 = 0; private static int current1 = -1;
private boolean firstTime = true;
private int key; private MultiPath mp;
public MultiPathClient() { super("Illustrating switch .. case");
array = new boolean[SIZE];
// fill with values false for (int i = 0; i < array.length; i++) { array[i] = false; }
mp = new MultiPath(array, count1, current1);
setSize(500, 600); setVisible(true); }
// ***** Student writes this method public void workWithSwitch(int value) { // Student code starts here:


// Student code ends here.
mp.setControl(false); mp.resetPath(); mp.setCount(0); mp.setCurrent(-1); }
public void startActivity() { boolean goodInput = false; while (!goodInput) { try { String answer = JOptionPane.showInputDialog(null, "Enter an integer");
if (answer != null) { key = Integer.parseInt(answer); goodInput = true; } else { System.exit(0); } } catch(Exception e) {} } if (goodInput) { firstTime = false; mp.setControl(true); workWithSwitch(key); } }
public static int getCount1() { return count1; }
public static int getCurrent1() { return current1; }
public static boolean [] getArray() { return array; }
private void animate(int index, int value) { try { mp.setCurrent(value); mp.setPath(index, true); mp.addToCount(); repaint(); Thread.sleep(100); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } }
public void paint(Graphics g) { super.paint(g); if (!firstTime) { mp.draw(g); } }
public static void main(String [] args) { MultiPathClient app = new MultiPathClient(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); PrintArrayT t = new PrintArrayT(app); t.start(); }
} /* MultiPathClient.java Anderson, Franceschi */
import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JOptionPane;
public class MultiPathClient extends JFrame { private final int SIZE = 6;
public static boolean [] array; private static int count1 = 0; private static int current1 = -1;
private boolean firstTime = true;
private int key; private MultiPath mp;
public MultiPathClient() { super("Illustrating switch .. case");
array = new boolean[SIZE];
// fill with values false for (int i = 0; i < array.length; i++) { array[i] = false; }
mp = new MultiPath(array, count1, current1);
setSize(500, 600); setVisible(true); }
// ***** Student writes this method public void workWithSwitch(int value) { // Student code starts here:


// Student code ends here.
mp.setControl(false); mp.resetPath(); mp.setCount(0); mp.setCurrent(-1); }
public void startActivity() { boolean goodInput = false; while (!goodInput) { try { String answer = JOptionPane.showInputDialog(null, "Enter an integer");
if (answer != null) { key = Integer.parseInt(answer); goodInput = true; } else { System.exit(0); } } catch(Exception e) {} } if (goodInput) { firstTime = false; mp.setControl(true); workWithSwitch(key); } }
public static int getCount1() { return count1; }
public static int getCurrent1() { return current1; }
public static boolean [] getArray() { return array; }
private void animate(int index, int value) { try { mp.setCurrent(value); mp.setPath(index, true); mp.addToCount(); repaint(); Thread.sleep(100); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } }
public void paint(Graphics g) { super.paint(g); if (!firstTime) { mp.draw(g); } }
public static void main(String [] args) { MultiPathClient app = new MultiPathClient(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); PrintArrayT t = new PrintArrayT(app); t.start(); }
} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* PrintArrayT * Anderson, Franceschi */
public class PrintArrayT extends Thread { boolean [] arr; MultiPathClient mp1;
public PrintArrayT( MultiPathClient m ) { arr = MultiPathClient.array; mp1 = m; }
public void run( ) { while( true ) { try { Thread.sleep( 500 ); } catch( Exception e ) {} mp1.startActivity( ); } } } /* PrintArrayT * Anderson, Franceschi */
public class PrintArrayT extends Thread { boolean [] arr; MultiPathClient mp1;
public PrintArrayT( MultiPathClient m ) { arr = MultiPathClient.array; mp1 = m; }
public void run( ) { while( true ) { try { Thread.sleep( 500 ); } catch( Exception e ) {} mp1.startActivity( ); } } }

Explanation / Answer

Below is the MultiPathClient.java where you requested the modification . The console output has been tested for all types of integer values as asked in the query .

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class MultiPathClient extends JFrame

{

private final int SIZE = 6;

public static boolean [] array;

private static int count1 = 0;

private static int current1 = -1;

private boolean firstTime = true;

private int key;

private MultiPath mp;

public MultiPathClient()

{

super("Illustrating switch .. case");

array = new boolean[SIZE];

// fill with values false

for (int i = 0; i < array.length; i++)

{

array[i] = false;

}

mp = new MultiPath(array, count1, current1);

setSize(500, 600);

setVisible(true);

}

// ***** Student writes this method

public void workWithSwitch(int value)

{

// Student code starts here:

switch(value){  

case 0: System.out.println("0 was entered"); animate(0, value);break;  

case 1: System.out.println("1 was entered");animate(1, value);break;

case 2: System.out.println("2 was entered");animate(2, value);break;

case 3: System.out.println("3 was entered");animate(3, value);break;

case 4: System.out.println("4 was entered");animate(4, value);break;

default:

if(value>4 ){

if(value %2 ==0) {

System.out.println("The invalid value "+ value + " was entered, which is greater than 4 and even");

}else{

System.out.println("The invalid value "+ value + " was entered, which is greater than 4 and odd");

}

}else if(value <0){

if(value %2 ==0) {

System.out.println("The invalid value "+ value + " was entered, which is less than 0 and even");  

}else{

System.out.println("The invalid value "+ value + " was entered, which is less than 0 and odd");

}

}

animate(-1, value);

}  

// Student code ends here.

mp.setControl(false);

mp.resetPath();

mp.setCount(0);

mp.setCurrent(-1);

}

public void startActivity()

{

boolean goodInput = false;

while (!goodInput)

{

try

{

String answer = JOptionPane.showInputDialog(null, "Enter an integer");

if (answer != null)

{

key = Integer.parseInt(answer);

goodInput = true;

}

else

{

System.exit(0);

}

}

catch(Exception e)

{}

}

if (goodInput)

{

firstTime = false;

mp.setControl(true);

workWithSwitch(key);

}

}

public static int getCount1()

{

return count1;

}

public static int getCurrent1()

{

return current1;

}

public static boolean [] getArray()

{

return array;

}

private void animate(int index, int value)

{

try

{

mp.setCurrent(value);

mp.setPath(index, true);

mp.addToCount();

repaint();

Thread.sleep(100);

}

catch (InterruptedException e)

{

System.out.println("IE Exception " + e.getMessage());

System.out.println(e.toString());

}

}

public void paint(Graphics g)

{

super.paint(g);

if (!firstTime)

{

mp.draw(g);

}

}

public static void main(String [] args)

{

MultiPathClient app = new MultiPathClient();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

PrintArrayT t = new PrintArrayT(app);

t.start();

}

}