2 Replies - 804 Views - Last Post: 13 June 2011 - 01:45 AM Rate Topic: -----

#1 radhig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-November 09

My Java code does not compile correctly.

Posted 12 June 2011 - 09:41 PM

My print class does not print :-/

import java.util.Scanner;
public class DummyCalculator{
public static void main (String Args[]){
  //use the input class to capture the inputs
  Input myInput = new Input(); //create the object
  myInput.captureNumbers();

  
  // use the addition class to add the numbers
  Addition myAddition = new Addition();
  int result = myAddition.sum(myInput.x, myInput.y);
  
  // use the modulus class the find the remainder of the numbers
  Modulus myModulus = new Modulus();
  int newResult = myModulus.remainder(myInput.x, myInput.y);
  // use the printer class to print the outputs
  Print p = new Print();
  p.print(result, newResult, myInput.x, myInput.y);
  
                     
}

}


  
//capture the inputs
class Input{
  //define instance variables
  int x;
  int y;
  
  void captureNumbers(){
  
  Scanner a = new Scanner(System.in);
  
  System.out.println("Enter the first number:");
  int x = a.nextInt();
  System.out.println("The number you typed in was:" + x);
  
  System.out.println("Enter the second number:");
  int y = a.nextInt();
  System.out.println("The number you typed in was:" + y);
  }
}

  
// define the addition class
class Addition{
  
  int sum(int b, int c){
  int total = b + c;
  return total;
  
    
  }
  
}
// define the modulus class
class Modulus{
  
  int remainder(int e, int g){
  //create instance of Input class
  int j = e%g;
  return j;
  
    
  }
  
}

class Print{
void print(int addition, int modulus, int firstNumber, int secondNumber){
System.out.println("Numbers keyed in are " + firstNumber + "and" + secondNumber);
System.out.println("Their sum is: " + addition);
System.out.println("Their remainder is:" + modulus);
  }

}




  



Is This A Good Question/Topic? 0
  • +

Replies To: My Java code does not compile correctly.

#2 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: My Java code does not compile correctly.

Posted 12 June 2011 - 11:36 PM

You said your code does not compile correctly. Which part? What error messages are you receiving upon compilation? Can you be a bit more specific? :)

One thing I noticed in your Input class is that you define instance variables x and y, but in your captureNumbers() method, you define local variables x and y which receive input from your Scanner object. So, your instance variables x and y are always going to be set to 0. This is a problem when you run this statement:

int result = myAddition.sum(myInput.x, myInput.y);


You're passing in the instance variables x and y to the sum method. But these two variables are always going to equal 0, so the number that is returned is going to be 0.

This shouldn't result in the compilation error that you reported, but it is something I noticed upon first glance. I have to go to work in the morning or else I'd stick around and look through your code in more detail. One of the more senior members will probably be around soon enough and get you on the right track. :)

This post has been edited by Codebug: 12 June 2011 - 11:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: My Java code does not compile correctly.

Posted 13 June 2011 - 01:45 AM

Hi

I noticed that you are calling variables across classes, ei. you have:
int result = myAddition.sum(myInput.x, myInput.y);


which means that you are creating public variables. As a rule of thumb, all your global variables in a class should be private, so you can't get access to them from outter classes. What you should do to get access to them from other classes, is to create accessor methods, also called getters.

So having an accessor method in your class with the private variable you wanna call, your code above will be:
int result = myAddition.sum(myInput.getX(), myInput.getY());



There might be a specific reason why you have public variables, in that case, just ignore my post, but otherwise I'd recommend to start making your variables private :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1