-
Calculator help.
ok im supposed to do a calculator and ive red the book over 20-30 times and this just doesnt make sense to me . my calculator isnt supposed to have graphics or nothing just a plain old dos calc. asks you wat operation you want to do. you type it in. the operation comes out nd u input the numbers and it solves it for you. i have the division code and the start of the calc. but idk how to put them together. can someone help. heres my code. for the calc and division.
Code:
import java.util.Scanner;
public class Calc
{
public static void main(String []args)
{
//creat a scanner object to read input
Scanner keyboard = new Scanner(System.in);
int multiply, add, subtract, divide, number ;
System.out.println("What operation would you like to do?:");
System.out.println ("multiply");
System.out.println ("add");
System.out.println ("subtract");
System.out.println ("divide");
System.out.println();
multiply = keyboard.nextInt();
add = keyboard.nextInt();
subtract = keyboard.nextInt();
divide = keyboard.nextInt();
}
}
and heres the division which i got from the book
Code:
import java.util.Scanner; //needed for scanner class
/**
*this program demonstrates the if-else statement
*/
public class Division
{
public static void main(String []args)
{
double number1, number2; // division operands
double quotient; // result of division
// creat a scanner objet for keyboard input
Scanner keyboard = new Scanner(System.in);
//get the first number
System.out.print("enter a number: ");
number1 = keyboard.nextDouble();
//get the 2nd number
System.out.print("divided by: ");
number2 = keyboard.nextDouble();
if (2 == 0)
{
System.out.println("division by zero is not possible.");
System.out.println("please run the program again and ");
System.out.println("enter a number other than zero");
}
else
{
quotient = number1 / number2;
System.out.println(number1);
System.out.println("divided by " + number2);
System.out.println("is " + quotient);
}
}
}
-
Do you know how to create and use methods? and how is your Calc class getting input? how can it tell what operation to perform?
-
Yes, first of all learn about methods in Java, how to define how to handle to have some process how to call methods in different locations and so on.
When you get fair knowledge about methods, you can easily implement all your classes as a method. Actually you can find out what your classes doing, which you have found from the book.