Hi All,

im trying to get an example to work on a main class passing variables to a class to then return the value.

This is the method for variables to be passed too

import java.lang.Math;

class CalcTest	{
	
	private int area;
	
	public int getArea(int height, int width)	{
		
		return area;
		
	}
}

the main class is

import java.lang.Math;

class CalcDrive	{

	public static void main(String[] arguments)	{
	
	CalcTest one = new CalcTest();
	
	int b = 7;
	int c = 12;
	int a = getArea(b c);
	
	System.out.println("the area is: " area);
	}
}

Please can you advise as to where im going wrong, ive been stuck on the for hours now.

Thanks

Recommended Answers

All 4 Replies

I would try

import java.lang.Math;

public class CalcDrive {

    public static void main(String[] arguments) {
    
    CalcTest one = new CalcTest();
    
    int b = 7;
    int c = 12;
    int a = one.getArea(b, c);
    
    System.out.println("the area is: " + a);//corrected variable name
    }
}



 class CalcTest{
    public CalcTest(){//you need a creator class
        
    }
   private int area;
   public int getArea(int height, int width){
        area= height*width;//calculation;
        return area;
   }
}

Although this seems like a weird thing to do just for area calculations. But whatever make us happy =)

Hi, Thanks for the quick reply.

Im fairly new to java so im just trying to try out different things so i can remember.

I understand its good practice to make variables private, then creating a method to use the private variable, the other classes to call that method.

The changes you have made now allow the program to compile BUT its giving a value of 0. Would it be possible for you to amend slightly to offer such an example?

Regards

Paul

wait, i found it.

Silly me. I forgot to actually add the calculation in the method.

Thanks very much

The changes you have made now allow the program to compile BUT its giving a value of 0. Would it be possible for you to amend slightly to offer such an example?

What would you like an example of? Just let me know and can explain anything you need to know (hopefuly). =)

When I compile/run the program the method returns 84 as the value of area . Are you using different values for the input?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.