Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds


  Asked By: Jeff    Date: Aug 08    Category: Java    Views: 855

hey can i get ur help guys plz i really need u
i dunno how to use the hell java thing
so i can i get ur help i have to make a calculator
here u r my assignment

1.Create a class Calculator in VisualAge with the following members:

Private Fields:
int number1;
int number2;

Public methods:
int add()
int sub()
int mul()
float div()

Calculator( ) //default constructor
Calculator(int x, int y)

2. Implement the above methods and constructors. In the default
constructor, set number1 and number2 to zero.

3. Handle the ArithmeticException in div ( ) method

4. Create a driver class to test the Calculator class.


package Calculator;

public class Calculator{

private int num1, num2; //private field

public Calculator(int x, int y) { //constructor
this.num1=x; this.num2=y;
public int add(){
return num1+num2;

public int sub(){
return num1-num2;

public int mul(){
return num1*num2;

public float div(){
return num1/num2;
/* if (num2==0) {
System.out.println("Undefined");}*/ //if it is divied by 0 the result
is undefined

package Calculator;

import java.io.*;
import java.math.*;

public class driver {

public static void main(String[] args) {
System.out.println("Enter the 1st #");
String n1="";
int num1=0;
System.out.println("Enter the 2nd #");
String n2="";
int num2=0;

BufferedReader br = new BufferedReader(new InputStreamReader
}catch(IOException e){
System.out.println("Input Error ");

BufferedReader br = new BufferedReader(new InputStreamReader
}catch(IOException e){
System.out.println("Input Error ");

Calculator Calc =new Calculator(num1); // Create an object.

Calculator Calc =new Calculator(num2); // Create an object.

int a = Calc.add();
int s = Calc.sub();
int m = Calc.mul();
float d = Calc.div();

System.out.println("add \t" + a);
System.out.println("sub \t" + s);
System.out.println("mul \t" + m);
System.out.println("div \t" + d);