Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Using Two Classes

i have no idea how to do this at all. i have to create a class called circle and have it able to receive a diameter and give the circumference and area. i have the two classes made and the equations ready, i just need to know how to pass information between classes.
Code:
```public static void main(String[] args)
{

}

public static int Circle()
{
int pi=3.14;
int diameter;
int circ;
int area;

}```

• I will provide you with some skeleton code, hopefully without doing most of the work for you.

First we'll setup a Circle class like you described in the outlilne and put some of your circle code in it:
Code:
```public class Circle {
int pi=3.14;
int diameter;
int circ;
int area;
}```
There's a couple small issues in the code above. pi (3.14) is not an integer (int), thus you should change it to "double". Also, it is quite possible that the circumference and area of the circle will not be exact integers as well, so maybe you should change these to "double" as well. I'll let you decide whether you wish to force the diameter and radius to be integers.

Since this hopefully will be a learning experience, let's take the above code and improve the style. There are two changes I believe will improve the style.

1. All member variables should be declared "private", since we want only the
Circle class to have direct access and the ability to modify these variables.

2. Since pi is a constant number (it's value never changes) we should declare pi as a variable that never changes. Along with this "good style" suggests that constant variable names are Uppercase.

Let's make these changes, including the changes to "double"
Code:
```public class Circle {
final static double PI = 3.14;
private int diameter;
private double circ;
private double area;
}```
Ok, we have our variables setup, now we should add a Constructor that accepts a diameter
Code:
```public class Circle {
final static double PI = 3.14;
private int diameter;
private double circ;
private double area;

//Constructor
public Circle(int d){
//Store d as the diameter
}
}```
Next, I would suggest making methods to return the Circumference and Area of the Circle. I'll give a skeleton of Area to give you an idea
Code:
```public double area(){

//Then return the area
return area;
}```
Next, we need a "main" method to run the code and make some circle objects
Code:
```public static void main(String[] args) {
//Make a Circle with Diameter of 3 units
Circle c = new Circle(3);

//Calculate and printout the Circumference
System.out.println("Circumference: " + c.circumference());

//Calculate and printout the Area
System.out.println("Area: " + c.area());
}```
Ok, let's put it all together
Code:
```public class Circle {

final static double PI = 3.14;
private int diameter;
private double circ;
private double area;

public Circle(int diameter){
//Store d as the diameter
}

public double area(){

//Then return the area
return area;

}

//***NEED TO ADD circumference METHOD HERE

//Main (execution) method
public static void main(String[] args) {
//Make a Circle with Diameter of 3 units
Circle c = new Circle(3);

//Calculate and printout the Circumference
//c.circumference is not implemented in this thread
System.out.println("Circumference: " + c.circumference());

//Calculate and printout the Area
System.out.println("Area: " + c.area());
}
}```
Hope that helps you structure your code.

Gox

• im sorry but i am lost. i am totally new to this so dont hate me :P

• The final code-block in my above post is roughly how you should/could setup your code. Just copy and paste it into a new file called Circle.java

The first part of my post is intended to give you step-by-step instructions on how to create a class
Code:
```public class Circle{
}```
How to add variables to that class
Code:
```public class Circle {
final static double PI = 3.14;
private int diameter;
private double circ;
private double area;
}```
How to add "methods" to a class to perform your Area and Circumference equations
Code:
```public double area(){

//Then return the area
return area;
}```
And how to add a "main" method to your class to run the code, and make a Circle object using the Circle class you implemented
Code:
```public static void main(String[] args) {
//Make a Circle with Diameter of 3 units
Circle c = new Circle(3);

//Calculate and printout the Circumference
System.out.println("Circumference: " + c.circumference());

//Calculate and printout the Area
System.out.println("Area: " + c.area());
}```
Again, the last code-block in my first post is just all these pieces put together in one Circle class. You just need to fill in the missing code in the Constructor and in the area method. Once done that you should be able to add the circumference method yourself.

I did my best to try and make it easy to follow, but admittedly I'm not a teacher. Go through it slowly and piece-by-piece and it hopefully will make some sense.

• thank you so much for the time and effort you put into helping me. really. but i just really dont understand this at all. what i dont understand is, once i prompt for the diameter, how do i move that info to the circle class and then bring it back to the main? if that makes sense

• There are a couple of ways you can set the Diameter info in the Circle class. In my above example I was storing the diameter when I make a Circle object. So something like this in the "main" method (I'll add a little for prompting):
Code:
```public static void main(String[] args) {
//Create a scanner to read user input
Scanner input = new Scanner(System.in);

//Prompt user for diameter
System.out.println("Enter Diameter:");

int userDiameter = input.nextInt();

//Now we construct a Circle and set the Diameter at the same time
Circle c = new Circle(userDiameter);
}```
EDIT: Now in my first two posts I didn't allow the ability to retrieve the Diameter of the Circle once it was created, but this can be easily accomplished. All we need is a method in the Circle class that "returns" the diameter
Code:
```public class Circle{
//All previous circle code would be here
...

//Add a method to get the Diameter of the Circle
public int getDiameter()
{
return diameter;
}```
Now we can access the diameter of the Circle from our main method like so:
Code:
```public static void main(String[] args) {
//Create a scanner to read user input
Scanner input = new Scanner(System.in);

//Prompt user for diameter
System.out.println("Enter Diameter:");

int userDiameter = input.nextInt();

//Now we construct a Circle and set the Diameter at the same time
Circle c = new Circle(userDiameter);

//Get and print the Diameter of the Circle using our new getDiameter method in the circle class
System.out.println("Diameter: " + c.getDiameter());
}```
Ok, I feel I'm getting pretty close to providing a full solution here. Read through the code in these posts, and actually code along while reading as it will help. Then mess around with it a bit to get a better idea of how things interact. If you still have problems post "your" code and we'll fix any remaining issues.

• okay i will try it from here and post back with the results. but just a quick question. do i just put my equations in the area method? or does each equation get its own method? how does that work?

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•