im trying to learn java from where i left off, somewhere around classes and class design and all that. im trying to learn without relying solely on a book, as i know most of it, but i wont actually learn how to use it without a book unless im writing it independently. i couldnt think of anything better to attempt, so im trying to make a calculator class. it compiles so i know that it will technically work, but im looking more for advice on how things are supposed to be and if i am doing it correct. thanks for any help.
also, i had alot of trouble with how to set up the scanner. i have never technically figured out how to use the scanner in a method. i tried to have one method to initialize the scanner, another method to gain the first input, and another to gain the second input, but for some reason this wouldnt work. i just scratched that whole deal till i could find a way to do it correct.
from what i understand, the main method is just supposed to be method calls. im having a little trouble putting everything into a method, so some advice there would also be appreciated
/**
*
* @author Nathan
*/
import java.util.Scanner;
public class Calculator
{
//initializer class variables
private double a;
private double b;
private double total;
//constructor
public Calculator(double firstNumber, double secondNumber)
{
a = firstNumber;
b = secondNumber;
}
//
//gets the value of a
public double getA()
{
return a;
}
//gets the value of b
public double getB()
{
return b;
}
//gets the value of total
public double getTotal()
{
return total;
}
//method to add the two inputs
public double add()
{
total = a+b;
return total;
}
//method to subtract the two inputs
public double sub()
{
total = a-b;
return total;
}
//method to multiply the two inputs
public double mult()
{
total = a*b;
return total;
}
//method to divide the two inputs
public double div()
{
total = a/b;
return total;
}
//i know that a toString method is supposed to output a
//string that represents the object, but im not really sure
//what to put to give a good depiction
public String toString()
{
return a + ", " + b;
}
}

New Topic/Question
Reply



MultiQuote











|