Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Multiple Class Calculator

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    Ulaanbaatar, Mongolia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple Class Calculator

    I am trying to write a Calculator. I must write the operations in one Class and the result in another. So this is what i did...

    public class Calc {
     
    	calc_main c=new calc_main();
    	private int add;
    	private int sub;
    	private int mul;
    	private int div;
     
    	public Calc(){
    		add=0;
    		sub=0;
    		mul=0;
    		div=0;
    	}
    	public void adder(int adds){
    	adds=c.number1+c.number2;
    	add=adds;
    	}
    	public int added(){
    		return add;
    		}
     
    	public void suber(int subs){
    	subs=c.number1-c.number2;
    	sub=subs;
    	}
    	public int subed(){
    		return sub;
    		}
     
    	public void muler(int muls){
    	muls=c.number1*c.number2;
    	mul=muls;
    	}
    	public int muled(){
    		return mul;
    		}
     
    	public void diver(int divs){
    	divs=c.number1/c.number2;
    	div=divs;
    	}
    	public int dived(){
    		return div;
    		}
    }

    public class calc_main {
     
    	public int number1;
    	public int number2;
    	public String oper;
     
    	public calc_main(){
    	Scanner num=new Scanner(System.in);
     
    	System.out.println("Enter first number:");
    		number1=num.nextInt();
    	System.out.println("Enter operation:");
    		oper=num.next();
    	System.out.println("Enter second number:");
    		number2=num.nextInt();
    	}
    }
     
    	public static void main(String[] args) {
    		calc_main c=new calc_main();
    		Calc r=new Calc();
    		if(c.oper=="+")
    			System.out.println(c.number1+"+"+c.number2+"="+r.added());
    		else if(c.oper=="-")
    			System.out.println(c.number1+"-"+c.number2+"="+r.subed());
    		else if(c.oper=="*")
    			System.out.println(c.number1+"*"+c.number2+"="+r.muled());
    		else if(c.oper=="/")
    			System.out.println(c.number1+"/"+c.number2+"="+r.dived());
     
    	}
    There are no errors in any lines. But when i run it, it asks me to enter the numbers and operator twice and just stop.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple Class Calculator

    and just stop.
    Does that mean the program exits?
    Try debugging the code by adding some println statements in the methods that are called so you can see where the execution flow is going. Print out a message when each method is entered and when it exits. That will show you what methods are being executed and in what order. If a message isn't printed, then you know that the method was not called.


    Also add an else statement at the end of the if/else if chain to print a message saying none of the if above statements were true.

    One problem I see is the use of == to compare Strings. The code needs to use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Multiple Class Calculator

    The two inputs results from the two constructor calls(you make the input operation in the constructor):
    First:
    public static void main(String[] args) {
    		calc_main c=new calc_main();

    and second:

    public class Calc {
     
    	calc_main c=new calc_main();

    Be careful, you now use two calc_main objects. With the first - in the main method - you check the calculation option with the second one - class var in Calc - you do some way of calculation, I think.

    BTW Thanks for the thread. I someway thought that due to the switch-string feature in java 1.7 the string comparisons now work in the == way. Dont know why.
    I use java 1.6 at the moment but this thread prevents me making bad mistakes in future

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Location
    Ulaanbaatar, Mongolia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Class Calculator

    Thanks guys. thanks to your advice i fixed my problems.
    with println debug i realized that operation performing objects were not called. so i fixed that.
    and asking to enter numbers twice was because i called the calc_main object twice.

    also glad to be help, even thou i had no idea about it

Similar Threads

  1. Best way to turn a single class into multiple classes
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2012, 11:57 AM
  2. Multiple methods in the same inner class?
    By tommyf in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 24th, 2012, 01:10 PM
  3. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 08:42 AM
  4. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM