Java Program Structure Problems

Only available on StudyMode
  • Download(s) : 332
  • Published : February 15, 2013
Open Document
Text Preview
PROGRAM STRUCTURE-SEQUENCE PROBLEMS

1.Write a Java class that prompts a student for the number of credit hours in which the student is enrolled, and the amount of money spent of books. Display, with full explanation, the student’s total fees. The total is Php 300 per credit hour, plus the amount for books, plus a Php 500 athletic fee.

SOURCE CODE:
import javax.swing.JOptionPane;
public class JStrucSeqProb1
{
public static void main(String[]args)
{
String numCreditHours, BookAmt;
double credithours , credithoursamt, bookamt, atlfee, total;

numCreditHours = JOptionPane.showInputDialog(null, "Enter the number of your Credit Hours\n(Php300/Credit Hours): ", "Credit Hours", JOptionPane.PLAIN_MESSAGE);
BookAmt = JOptionPane.showInputDialog(null, "Enter the amount of Book Spent: ", "Book Amount", JOptionPane.PLAIN_MESSAGE);

//converting String to Integer
credithours = Double.parseDouble(numCreditHours);
bookamt = Double.parseDouble(BookAmt);
atlfee = 500;
//computation
credithoursamt = credithours * 300;
total = credithoursamt + bookamt + atlfee;

JOptionPane.showMessageDialog(null, "Php300/Credit Hours Amount: Php" + credithoursamt + "\nBook Amount: Php" + bookamt + "\nAthletic Fee: Php" + atlfee + "\n\nTotal Amount: Php" + total, "Results", JOptionPane.INFORMATION_MESSAGE);

}
}

OUTPUT LAYOUT:

Fig.1 Fig.2

Fig.3

2.Write a class that accepts a user’s hourly rate of pay and the number of hours worked. Display the user’s gross pay, the withholding tax (15% of gross pay), and the net pay (gross pay-withholding).

SOURCE CODE:
import javax.swing.JOptionPane;
public class JStrucSeqProb2
{
public static void main(String[]args)
{
String numHours;
double hours, hrsamt, gross, taxpercent, taxamt, net;

numHours = JOptionPane.showInputDialog(null, "Enter the number of hours worked: ", "Hours Worked", JOptionPane.PLAIN_MESSAGE);

hours = Double.parseDouble(numHours);
hrsamt = 57;
taxpercent = .15;

gross = hours * hrsamt;
taxamt = gross * taxpercent;
net = gross - taxamt;

JOptionPane.showMessageDialog(null, "Gross Pay Amount: Php " + gross + "\nTax Amount: Php " + taxamt + "\nNet Pay Amount: Php " + net, "Results", JOptionPane.INFORMATION_MESSAGE);
}
}

OUTPUT LAYOUT:

Fig.1 Fig.2

3.Write a class that calculates and displays the conversion of an entered number of dollars into currency denominations – 20s, 10s, 5s, and 1s.

SOURCE CODE:
import javax.swing.JOptionPane;
public class JStrucSeqProb3
{
public static void main(String[]args)
{
String strValue;
int nValue, n20s, n10s, n5s, n1s;

strValue = JOptionPane.showInputDialog(null, "Enter a dollar amount: ", "Denomination Conversion", JOptionPane.PLAIN_MESSAGE);
nValue = Integer.parseInt(strValue);

n20s = nValue / 20;
n10s = nValue / 10;
n5s = nValue / 5;
n1s = nValue / 1;

JOptionPane.showMessageDialog(null, "20s = " + n20s + "\n10s = " + n10s + "\n5s = " + n5s + "\n1s = " + n1s, "Denomination Conversion", JOptionPane.INFORMATION_MESSAGE);
}
}

OUTPUT LAYOUT:

4.Write a program that accepts a temperature in Fahrenheit from a user and converts it to Celsius by subtracting 32 from the Fahrenheit value and multiplying the result by 5/9. Display both values.

SOURCE CODE:
import javax.swing.JOptionPane;
public class JStrucSeqProb4
{
public static void main(String[]args)
{
String Fahr;
float fahr, celsius;

Fahr = JOptionPane.showInputDialog(null, "Fahrenheit:", "Temperature Converter", JOptionPane.PLAIN_MESSAGE);
fahr = Float.parseFloat(Fahr);

celsius = ((fahr-32)*5)/9;

JOptionPane.showMessageDialog(null, "Fahrenheit: " + fahr + "\nCelsius: " + celsius, "Conversion Result", JOptionPane.INFORMATION_MESSAGE);
}
}

OUTPUT LAYOUT:

Fig.1 Fig.2
5.Given the list price of an item and the rate of...
tracking img