Share
Explore BrainMass

Assistance with Java

First Problem
I need help answering the following questions. I tried to do this in Java but I couldn't get it to execute.
int numValue = 10;
int answer = 0;
switch(numValue)
{

case 5: answer += 5;
case 10: answer += 10;
case 15: answer += 15;
break;
case 20: answer += 20;
case 25: answer += 25;
default: answer = 0;
break;
}
System.out.println("Answer: " + answer);

• What is the value of answer if the value of numValue is 10?

• What is the value of answer if the value of numValue is 20?

• What is the value of answer if the value of numValue is 5?

• What is the value of answer if the value of numValue is 17?

• Is the break statement in the default case needed? Explain?

Second Problem - need assistance with the area highlighted in yellow. Design the logic and write the rest of the program using a nested statement. They are using JOptionPane.showInputDialog.
Productivity Score Bonus
<= 30 $25
31-79 $50
80-199 $100
>=200 $200

// EmployeeBonus.java - This program calculates an employee's productivity bonus.

import javax.swing.*;

public class EmployeeBonus
{
public static void main(String args[])
{
// Declare and initialize variables here.
String employeeName;
double numTransactions;
String transactString;
double numShifts;
String shiftString;
double dollarValue;
String dollarString;
double score;
double bonus;
final double BONUS_1 = 25.00;
final double BONUS_2 = 50.00;
final double BONUS_3 = 100.00;
final double BONUS_4 = 200.00;

// This is the work done in the housekeeping() method
employeeName = JOptionPane.showInputDialog("Enter employee's name: ");
shiftString = JOptionPane.showInputDialog("Enter number of shifts: ");
transactString = JOptionPane.showInputDialog("Enter number of transactions: ");
dollarString = JOptionPane.showInputDialog("Enter transactions dollar value: ");

numShifts = Double.parseDouble(shiftString);
numTransactions = Double.parseDouble(transactString);
dollarValue = Double.parseDouble(dollarString);

// This is the work done in the detailLoop() method

// Write your code here (Design the logic and write the rest of the program
//using a nested if statement.

// This is the work done in the endOfJob() method
// Output.
System.out.println("Employee Name: " + employeeName);
System.out.println("Employee Bonus: $" + bonus);

System.exit(0);
}
}

Attachments

Solution Preview

The first part of this solution deals with the Java switch statement.

The switch statement in Java is a way of choosing between several
alternatives. It is similar to an if/else statement but it easily
allows for several options. The following code shows a switch
statement:

int numValue = 10;
int answer = 0;
switch(numValue)
{

case 5: answer += 5;
case 10: answer += 10;
case 15: answer += 15;
break;
case 20: answer += 20;
case 25: answer += 25;
default: answer = 0;
break;
}
System.out.println("Answer: " + answer);

The control flow for a switch statement is that execution passes to
the first matching case and then falls through each set of statements
till a break statement is encountered. We can see that with the
following questions.

What is the value of answer if the value of numValue is 10?

In this situation we match the "case = 10" statement. This leads us
to execute answer += 10; ...

Solution Summary

The assistance with Java statements are determined.

$2.19