Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

A Calc - Java Code Bank


A Calc
a calculator that let the User to choose what to do with the number
                // main class
class Project {
       public static void main(String args[]){
    	   
    	   
    	   System.out.println("Press 1 to add");  
    	   System.out.println("Press 2 to subtract");
    	   System.out.println("Press 3 to Divide");
    	   System.out.println("Press 4 to Multiply");
    	   
    	   Scanner  add = new Scanner(System.in);
    	   String add1 = add.nextLine();
    	   char add2 = add1.charAt(0);
    	   
    	   
    	   
    	   if(add2=='1'){
    		   System.out.println("Enter your numbers");
    		   Project1 proob = new Project1();
    		   proob.addnum(5,6);
    		   
    	   }else if(add2=='2'){
    		   System.out.println("Enter your numbers");
    		   Subtract sub = new Subtract();
    		   sub.subnum(0,0);
    	   }else if(add2=='3'){
    		   System.out.println("Enter your numbers");
    		   Divide div = new Divide();
    		   div.divnum(0,0);
    		   
    	   }else if(add2=='4'){
    		   System.out.println("Enter your numbers");
    		   Multiply mult = new Multiply();
    		   mult.multnum(0,0);
    	   }
    	   
    	  
       }
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

//the adding class

import java.util.Scanner;
public class Project1 {
	private Scanner  num1 = new Scanner(System.in);
	private Scanner num2 = new Scanner(System.in);
	private int total;
	
	public void addnum(int n1, int n2){
		
		n1 = num1.nextInt();
		n2 = num1.nextInt();
		total = n1+n2;
		
		System.out.println("Your total is " + total);
	}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Subtracting class

import java.util.*;
public class Subtract {
	Scanner gol = new Scanner(System.in);
	private Scanner  num1 = new Scanner(System.in);
	private Scanner num2 = new Scanner(System.in);
	private int total;
	
	public void subnum(int n1, int n2){
		
		n1 = num1.nextInt();
		n2 = num1.nextInt();
		total = n1-n2;
		
		System.out.println("Your total is " + total);
     }
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
//  Multiply class

import java.util.*;
public class Multiply {
	private Scanner  num1 = new Scanner(System.in);
	private Scanner num2 = new Scanner(System.in);
	private int total;
	
	public void multnum(int n1, int n2){
		
		n1 = num1.nextInt();
		n2 = num1.nextInt();
		total = n1*n2;
		
		System.out.println("Your total is " + total);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Divide class

import java.util.*;
public class Divide {
	Scanner gol = new Scanner(System.in);
	private Scanner  num1 = new Scanner(System.in);
	private Scanner num2 = new Scanner(System.in);
	private int total;
	private int mod;
	
	public void divnum(int n1, int n2){
		
		n1 = num1.nextInt();
		n2 = num1.nextInt();
		total = n1/n2;
		mod = n1%n2;
		
		System.out.println("Your total is " + total+ " and a remainder of " + mod);
}
}
            
Comments
newbee's avatar
newbee 12 years ago

why don't you just add the nos. in the if-else itself , and not create methods for each and every one ?