Need help on java H/W. Thanks I think the teacher wants me to add the jframe to
ID: 3828427 • Letter: N
Question
Need help on java H/W. Thanks
I think the teacher wants me to add the jframe to another class instead of having it all in one class but I don't know how to do that.
Here is the code for the previous assignemt, the one we have to update for this assignment.
import javax.swing.JOptionPane;
public class Income {
public static double calculateTax(double income)
{
double tax = 0;
if(income > 0 && ++income <= 6000){
tax = income * 0.1;
}
else if(income >= 6001 && ++income <= 27950){
tax = income * 0.15;
}
else if(income >= 27951 && ++income <= 67700){
tax = income * 0.27;
}
else if(income >= 67701 && ++income <= 141250){
tax = income * 0.30;
}
else if(income >= 141251 && ++income <= 307050){
tax = income * 0.35;
}
else if(income > 307050){
tax = income * (38.6/100);
}
return tax;
}
public static void main(String args[]){
double income;
double tax = 0;
income = Integer.valueOf(JOptionPane.showInputDialog("Please enter $ amount"));
String result = "";
if(income<=0)
{
result = "You entered an invalid $ amount.";
}
else
tax = calculateTax(income);
if(result == "")
result = String.format("Tax owed $%.2f", tax);
JOptionPane.showMessageDialog(null, result);
}
}
Explanation / Answer
I have made changes to the program as you requested. I have written a seperate class which contains the logic for calculating tasks. Aslo I have used setters and getters methods to access the class variables. Here is the modified code.
==========================================
import javax.swing.JOptionPane;
class ComputeTax { // creating a separate class which contains logic for calculating tax.
double tax;
public void ComputeTax(){ // Using constructor to intialize variable tax
tax=0;
}
// Using setters and getters to access variables
public void setTax(double tax) {
this.tax = tax;
}
public double getTax() {
return tax;
}
// Your CalculateTax method comes here
public double calculateTax(double income)
{
if(income > 0 && ++income <= 6000){
setTax(income * 0.1); // Using setTax method to assign result
}
else if(income >= 6001 && ++income <= 27950){
setTax(income * 0.15); // Using setTax method to assign result
}
else if(income >= 27951 && ++income <= 67700){
setTax(income * 0.27); // Using setTax method to assign result
}
else if(income >= 67701 && ++income <= 141250){
setTax(income * 0.30); // Using setTax method to assign result
}
else if(income >= 141251 && ++income <= 307050){
setTax(income * 0.35); // Using setTax method to assign result
}
else if(income > 307050){
setTax(income * (38.6/100)); // Using setTax method to assign result
}
return tax;
}
}
public class Income{
public static void main(String args[]){
double income;
double tax = 0;
ComputeTax obj = new ComputeTax(); // Creating an object for the class ComputeTax
income = Integer.valueOf(JOptionPane.showInputDialog("Please enter $ amount"));
String result = "";
if(income<=0)
result = "You entered an invalid $ amount.";
else
tax = obj.calculateTax(income);
if(result == "")
result = String.format("Tax owed $%.2f", tax);
JOptionPane.showMessageDialog(null, result);
}
}
===============================
Compile the java program using comand javac Income.java and run the program using command java Income
You should get the same output as mentioned in the question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.