Hi!(:
Sorry, I'm totally new and have no real idea about what I'm supposed to tell you. I just really need some help on my java homework so anything would be much appreciated. (:
I feel like I'm already asking for so much.. so I'm really sorry, but can someone explain what local variables, instance variables, class variables, constants & methods are and how I would spot them in my code?
& thanks again. :]
Also, this is the error that I keep getting:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method add(Component) in the type Container is not applicable for the arguments (String)
The method add(String, Component) in the type Container is not applicable for the arguments (String, int)
The method add(Component) in the type Container is not applicable for the arguments (String)
at HwkAssignment2Appl.<init>(HwkAssignment2Appl.java:62)
at HwkAssignment2Appl.main(HwkAssignment2Appl.java:45)
& On to the (not so) wonderful code!
//bring in the package swing components
import javax.swing.*;
public class HwkAssignment2Appl extends JFrame
{
/**
* @param args
*/
//create object of JPanel
JPanel mainPanel = new JPanel();
//create 12 label objects for prompts that let user input three numbers. These three numbers are then added together and then multiplied together to get the tax that would be payed for these objects. The total price of purchase with tax is also displayed. The programmer name is also shown.
JLabel firstNumberPromptLabel = new JLabel("First Number");
JLabel firstNumberLabel = new JLabel(" ");
JLabel secondNumberPromptLabel = new JLabel("Second Number");
JLabel secondNumberLabel = new JLabel(" ");
JLabel thirdNumberPromptLabel = new JLabel("Third Number");
JLabel thirdNumberLabel = new JLabel(" ");
JLabel resultPromptLabel = new JLabel("Result Number");
JLabel resultLabel = new JLabel(" ");
JLabel nameLabel = new JLabel("Programmer Name: Hira Rizvi");
JLabel taxNumberLabel = new JLabel("Tax of items at 9.75% tax rate");
JLabel taxLabel = new JLabel(" ");
//set the tax rate to .0925
final double TAX_RATE = .0925;
public static void main(String[] args)
{
// create an object of the class and set the close of the X button
HwkAssignment2Appl myApp = new HwkAssignment2Appl();
myApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public HwkAssignment2Appl()
{
//add label objects to the panel
mainPanel.add(firstNumberPromptLabel);
mainPanel.add(firstNumberLabel);
mainPanel.add(secondNumberPromptLabel);
mainPanel.add(secondNumberLabel);
mainPanel.add(thirdNumberPromptLabel);
mainPanel.add(thirdNumberLabel);
mainPanel.add(resultPromptLabel);
mainPanel.add(resultLabel);
mainPanel.add("nameLabel");
mainPanel.add("taxNumberLabel", getDefaultCloseOperation());
mainPanel.add("taxLabel");
//Create 3 String objects to store in the user entry
String num1String, num2String, num3String;
//create 4 integers for the user entries and result
double num1Integer, num2Integer, num3Integer, resultInteger;
//create 2 integers for the sum of three integers inputed by user(additionInteger) and one for the sum of additionInteger and tax(taxInteger)
double additionInteger, taxInteger;
//JOption dialog returns the user entry
num1String = JOptionPane.showInputDialog(null, "Please enter the number");
num2String = JOptionPane.showInputDialog(null, "Please enter the number");
num3String = JOptionPane.showInputDialog(null, "Please enter the number");
//Convert from String object to integer
num1Integer = Integer.parseInt(num1String);
num2Integer = Integer.parseInt(num2String);
num3Integer = Integer.parseInt(num3String);
//add the numbers
additionInteger = num1Integer + num2Integer + num3Integer;
//multiply additionInteger by TAX_RATE in order to get the tax of items
taxInteger = additionInteger * TAX_RATE;
//add taxInteger and additionInteger to get the result
resultInteger = additionInteger + taxInteger;
//display the variables in the label objects
firstNumberLabel.setText(num1String);
secondNumberLabel.setText(num2String);
thirdNumberLabel.setText(num3String);
resultLabel.setText("" + resultInteger);
nameLabel.setText("Programmer: Hira Rizvi");
//add the panel object to the JFrame object
add(mainPanel);
//set the size and visibility of the JFrame
setSize(300,300);
setVisible(true);
}
}