I am new to java and I have an assignment to write a circle calculator program that computes the area, circumference and diameter of a circle. The program will accept input number (radius) and that number will be used to get the area, circumference and diameter of a circle. This time you are going to use separate methods for each computation. For example, to get the value of area you need to write method area that has radius parameter and returns double value.
I'm not sure if my code is right, I am getting zero values.
import java.util.Scanner;
class Circle {
static Scanner sc = new Scanner(System.in);
private double radius;
public double getRadius() {
System.out.print("Enter the radius: ");
int radius = sc.nextInt();
return radius;
}
public void setRadius(double radius) {
}
public double diameter() {
double diameter = 2 * radius;
return diameter;
}
public double area() {
double area = Math.PI * (radius * radius);
return area;
}
public double circumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
public class Methods {
public static void main(String[] args) {
Circle circleTest = new Circle();
System.out.printf("Radius: %.1f %n", circleTest.getRadius());
System.out.printf("Diameter: %.1f %n", circleTest.diameter());
System.out.printf("Area: %.1f %n", circleTest.area());
System.out.printf("Circumference: %.1f %n", circleTest.circumference());
}
}
Inside your getRadius method you are setting a local variable to the value entered by the user:
int radius = sc.nextInt();
When you calculate the diameter, area, circumference you're using an instance variable - for example:
double diameter = 2 * radius;
Since you never set the instance variable its value by default is 0.
Related
While making a circle class program. I faced an error in my driver section. The error is, Exception in thread "main" java.lang.Error: Unresolved compilation problems:
LastLab cannot be resolved to a type
LastLab cannot be resolved to a type
at Task1.Mainclass.main(Mainclass.java:18)
This is my method and data field code
public class Circle {
private final double PI = 3.14159;
private double radius;
public void Circle(double rad) {
radius = rad;
}
public void setRadius(double rad) {
radius = rad;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
This is my driver code
public class Mainclass {
public static void main(String... args) {
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the radius from user;
System.out.print("Enter the radius of your circle:");
double radius = keyboard.nextDouble();
// Create a circle object.
LastLab circle = new LastLab();
// Get data from circle and display it
System.out.println("The circle's area is: " + circle.getArea());
System.out.println("The circle's diameter is: " + circle.getDiameter());
System.out.println("The circle's circumference is: " + circle.getCircumference());
}
}
Circle circle = new Circle();
You have incorrect declaration: LastLab circle = new LastLab();
And you have incorrectly declared constructor for Circle. Check this out:
public class Circle {
private static final double PI = 3.14159;
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
public class Mainclass {
public static void main(String... args) {
Scanner sca = new Scanner(System.in);
System.out.print("Enter the radius of your circle: ");
Circle circle = new Circle(sca.nextDouble());
System.out.format(Locale.ENGLISH, "The circle's area is: %.2f\n", circle.getArea());
System.out.format(Locale.ENGLISH, "The circle's diameter is: %.2f\n", circle.getDiameter());
System.out.format(Locale.ENGLISH, "The circle's circumference is: %.2f\n", circle.getCircumference());
}
}
I am currently working on a project and i keep getting error messages. I am stuck and have contacted many people (including my instructor), and I have now turned to you guys.
Here is my code so far.
public class Circle
private int radius = getRadius();
private double area = getArea();
public Circle(int r)
{
r = radius;
}
public int getRadius()
{
return radius;
}
public double getArea(int r)
{
return area = Math.PI * r * r;
}
}
/
java.util.Scanner;
public class CircleTest
{
public CircleTest()
{
int radius = getRadius();
double area = getArea(r);
}
public static void main (String[] args)
{
Scanner kboard = new Scanner(System.in);
System.out.print("Give the radius of a circle. ");
String area = kboard.nextLine();
System.out.println("The area of the circle is... " +
area);
System.out.println();
kboard.close();
}
}
C:\Users\jthom\My Work\Circle\src\CircleTest.java:18: error: cannot find symbol
double area = getArea(r);
symbol: variable r
location: class CircleTest
.
C:\Users\jthom\My Work\Circle\src\CircleTest.java:33: error: cannot find symbol
System.out.println("The area of the circle is... " + area);
symbol: variable area
location: class CircleTest
2 errors
Let's start with class Circle. This is your original code:
public class Circle
private int radius = getRadius();
private double area = getArea();
public Circle(int r)
{
r = radius;
}
public int getRadius()
{
return radius;
}
public double getArea(int r)
{
return area = Math.PI * r * r;
}
}
For your variables, you don't need "area", as it is calculated. Also, you shouldn't be assigning radius to anything except in the constructor:
private int radius;
//private double area = getArea(); <-- don't need this variable at all
You pass in "r" to the constructor, but then incorrectly try to assign the "radius" value to it. This is backwards; you should be assigning the "r" value to "radius" instead:
public Circle(int r)
{
radius = r;
}
Finally, in getArea(), you don't need the radius passed in, or "area"; just return the calculated value (using the stored value in "radius", not "r"):
public double getArea()
{
return Math.PI * radius * radius;
}
Put all together, your Circle class should look more like:
public class Circle
private int radius;
public Circle(int r)
{
radius = r;
}
public int getRadius()
{
return radius;
}
public double getArea()
{
return Math.PI * radius * radius;
}
}
Over in CircleTest, you should first get the radius from the user, then pass that to the constructor of Circle. Finally, with your instance of Circle, call its getRadius() and getArea() methods.
You have not defined variable r and area ,to help solve this
replace double area = getArea(r); to double area = circle.getArea(radius);
and
System.out.println("The area of the circle is... " + area); with System.out.println("The area of the circle is... " + circle.getArea(Integer.parseInt(area)));
here the circle is an object of Class circle
Circle circle = new Circle(radius) //Note: circle cannot be accessed from main function create another object for class circle again in main
it should help
edit: here is link about the error What does a "Cannot find symbol" compilation error mean?
So, I have a twoparter question. The first part I did, which was creating an interface and then utilizing that interface in two different classes. However the second part of the question has me stumped. I'm not understanding the logic aspect of it. It feels as though the question order should be in reverse. This is two things I need to do to finish up the last portion of the assignment. If anyone here could give me just some tiny guidance as to to the direction I should be taking I'd greatly appreciate it.
Part II:
(1)Create an array of Calculatable objects in main and call the sumCalculate method.
For the most part I understand this, I already began doing it but decided to start working on (2) since that was way more difficult for me. It's basically creating an array of Calculatable objects of a certain size ( I chose 5) and populating it with different calculatable objects ( could be rectangles or squares in my example). But the second half of this question confuses me? Am I calling the sumCalculate method that I'm GOING to be making in question 2? Or am I calling it before I even make the (2) method.
(2)Make a method that accepts an array of Calculatable objects and sums up the values that are returned by each object's call to calculate.
What I'm trying to figure out here in question (2) is this. When it asks me to make the method? Does this mean that I'm making a new method in interface called sumCalc for example, that has parameters that accepts an array of calculatable objects? And then as far as summing up the values that are returned. I'd assume that I'd be adding the calculation double that is returned by calculate methods.
I'd ask my professor but this professor I decided to take has made it a habit of being excessively difficult to reach. Sorry to bother you guys with what is most likely an elementary and not difficult question.
interface Calculatable {
public double calculate(int x);
}
class square implements Calculatable {
public double side;
square(double side){
this.side = side;
}
public double getside(){
return side;
}
public double calculate(int x) {
double perimeter = side * 4;
System.out.println("This calculate method will output the perimeter divided by the parameter x");
double calculation = perimeter / x;
System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
return calculation;
}
}
public class rectangle implements Calculatable {
public double length;
public double width;
rectangle(double length , double width){
this.length = length;
this.width = width;
}
public double getlength(){
return length;
}
public double getwidth(){
return width;
}
public double calculate(int x) {
double perimeter = 2 * (length + width);
double calculation = 2 * perimeter;
System.out.println("This will return the perimeter of the rectangle times the x paramater");
System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
return calculation;
}
public static void main(String [] args){
Calculatable perimeter1 = new rectangle(20.5 , 50);
perimeter1.calculate(5);
Calculatable perimeter2 = new square(10.5);
perimeter2.calculate(2);
Calculatable[] perimetersums = new Calculatable[5];
perimetersums[0] = new rectangle(20.5 , 50);
perimetersums[1] = new rectangle(10 , 25);
}
}
I would create sumCalculate beside main method and be over with it. Lesson is to implement an interface method and use it too.
And beside that I suggest reading Java naming convention and correcting your code accordingly.
The way I understand it you should not change the Interface, especially if the Interface was provided to you!
Just write your sumCalculate below your main method like this
private static double sumCalculate(Calculateable[] c) {
// do your sum up and return the result
}
and call it in your main method like this
double sum = sumCalculate(perimetersums);
few confusion is in my mind.. In the implementation rectangle your are not using the x in the calculate method. I changed the whole class structure a little bit.. Please look.. I think it will help you...
public interface Calculatable {
public double calculate(int x);
public double getPerimeter();
}
public class square implements Calculatable {
public double side;
private double perimeter;
square(double side){
this.side = side;
}
public double getside(){
return side;
}
public double calculate(int x) {
double perimeter = side * 4;
System.out.println("This calculate method will output the perimeter divided by the parameter x");
double calculation = perimeter / x;
System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
this.perimeter=calculation;
return calculation;
}
#Override
public double getPerimeter() {
// TODO Auto-generated method stub
return perimeter;
}
}
public class rectangle implements Calculatable {
public double length;
public double width;
private double perimeter;
rectangle(double length , double width){
this.length = length;
this.width = width;
}
public double getlength(){
return length;
}
public double getwidth(){
return width;
}
public double calculate(int x) {
double perimeter = 2 * (length + width);
double calculation = 2 * perimeter;
System.out.println("This will return the perimeter of the rectangle times the x paramater");
System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
this.perimeter=calculation;
return calculation;
}
#Override
public double getPerimeter() {
// TODO Auto-generated method stub
return perimeter;
}
}
public class MainMethod {
public static void main(String[] args) {
Calculatable perimeter1 = new rectangle(20.5 , 50);
perimeter1.calculate(5);
Calculatable perimeter2 = new square(10.5);
perimeter2.calculate(2);
Calculatable[] perimetersums = new Calculatable[5];
perimetersums[0] = perimeter1; //new rectangle(20.5 , 50);
perimetersums[1] = perimeter2;// new rectangle(10 , 25);
System.out.println("the sum is= "+sumCalculate(perimetersums));
}
private static double sumCalculate ( Calculatable[] perimetersums)
{
double sum=0.0;
for(int i=0;i<perimetersums.length;i++)
{
Calculatable cal=perimetersums[i];
if(cal!=null)
sum=sum+cal.getPerimeter();
}
return sum;
}
}
I changed the class structure a little bit...
public interface Calculatable {
public double calculate();
}
public class square implements Calculatable {
private final int x=2;
public double side;
square(double side){
this.side = side;
}
public double getside(){
return side;
}
public double calculate() {
double perimeter = side * 4;
System.out.println("This calculate method will output the perimeter divided by the parameter x");
double calculation = perimeter / x;
System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
return calculation;
}
}
public class rectangle implements Calculatable {
private final int x=5;
public double length;
public double width;
rectangle(double length , double width){
this.length = length;
this.width = width;
}
public double getlength(){
return length;
}
public double getwidth(){
return width;
}
public double calculate() {
double perimeter = 2 * (length + width);
double calculation = 2 * perimeter;
System.out.println("This will return the perimeter of the rectangle times the x paramater");
System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
return calculation;
}
}
public class MainMethod {
public static void main(String[] args) {
Calculatable perimeter1 = new rectangle(20.5 , 50);
// perimeter1.calculate(5);
Calculatable perimeter2 = new square(10.5);
//perimeter2.calculate(2);
Calculatable[] perimetersums = new Calculatable[5];
perimetersums[0] = new rectangle(20.5 , 50);
perimetersums[1] = new rectangle(10 , 25);
System.out.println("the sum is= "+sumCalculate(perimetersums));
}
private static double sumCalculate ( Calculatable[] perimetersums)
{
double sum=0.0;
for(int i=0;i<perimetersums.length;i++)
{
Calculatable cal=perimetersums[i];
if(cal!=null)
{
sum=sum+cal.calculate();
}
}
return sum;
}
}
I have been working on this for days and can't get the program to run. I have to name 3 fields, and utilize setRadius() getRadius(). I also must set and compute all fields in setRadius(). At first I had no main, but kept getting error. I included main, program compiles, but won't run. Please help.
public class Circle
{
public static void main(String[] args)
{
Circle myCircle = new Circle();
}
double radius;
double diameter;
double area;
public void Circle()
{
radius= 1;
diameter = radius * radius;
area = Math.PI * radius * radius;
}
public void setRadius()
{
radius = 1;
diameter = radius * radius;
area = Math.PI * (radius * radius);
}
public double getRadius()
{
return radius;
}
public void display()
{
System.out.println("The radius is " + radius);
System.out.println("The diameter is " + diameter);
System.out.println("The area is " + area);
}
}
Like someone said in comments your constructor is not valid.
your setRadius shoud get an parameter
and call display after construction
public class Circle {
public static void main(String[] args) {
Circle myCircle = new Circle();
myCircle.display();
}
double radius;
double diameter;
double area;
public Circle() {
radius = 1;
diameter = radius * radius;
area = Math.PI * radius * radius;
}
public void setRadius(double radius) {
this.radius = radius;
diameter = radius * radius;
area = Math.PI * (radius * radius);
}
public double getRadius() {
return radius;
}
public void display() {
System.out.println("The radius is " + radius);
System.out.println("The diameter is " + diameter);
System.out.println("The area is " + area);
}
}
Main is the entry point of your program, whatever you include in it will run. Your program is running it's just not doing anything (except creating a circle). If you want the program to display those messages you need to call display() inside main.
public static void main(String[] args)
{
Circle myCircle = new Circle();
myCircle.display();
}
add myCircle.display(); to main.
It works. But only doing you make here is creting new object.
Add some more operations for your object.
public static void main(String[] args)
{
Circle myCircle = new Circle();
myCircle.setRadius();
myCircle.display();
}
UPD: Also you have some mistakes:
diametr = 2 * radius (not radius * radius)
public void Circle() - is a function, not constructor. It's bad practice to use methods with the same name as contructor. So if you want it be a constructor remove 'void'
It will be more flexible if you will use setRadius method like that:
public void setRadius(double r)
{
radius = r;
diameter = 2 * radius;
area = Math.PI * radius * radius;
}
My code compiles, but the output won't show. I think my methods are all correct, but I have no idea why my output will not display. (NB: beginner in Java)
import java.util.Scanner;
import java.lang.Math;
public class Lab8 {
public static void main(String[] args) {
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = in.nextDouble();
System.out.print("Enter height: ");
height = in.nextDouble();
}
public static double Calculations(double radius, double height) {
double surfaceArea = (2 * Math.PI * radius * radius) + (2 * Math.PI * radius * height);
return surfaceArea;
}
public static double calculations(double radius, double height) {
double volume = Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume) {
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
You need to call your method inside main to print the value.
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
Fixed Code:
import java.util.Scanner;
import java.lang.Math;
public class Lab8
{
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
}
public static double Calculations(double radius, double height)
{
double surfaceArea= (2 * Math.PI * radius * radius) + (2 * Math.PI *radius* height);
return surfaceArea;
}
public static double calculations(double radius,double height)
{
double volume= Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume)
{
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
Try calling one of your Calculations method in your main method, post taking the input such as:
System.out.println(Lab8.Calculations(radius,height));
In main you miss the calculate and output method call
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
// call this
System.out.println("area " + output(Calculations(radius,height)));
}
You don't actually call your methods.
In main, after you get the radius and height, call Calculations and calculations respectively, with the appropriate arguments.
Here's an example:
surfaceArea = Calculations(radius, height);
volume = calculations(radius, height);
You didn't call any of your helper functions within main(...). Try adding a call to output or calculations within your main function.
In particular, something like
output(Calculations(radius, height), calculations(radius, height));
will probably suffice.
As you learn Java, it may be helpful to begin applying descriptive names to your functions, such as CylinderSurfaceArea to help keep confusion to a minimum both for you and for anyone who might read code you have written.