dimanche 9 octobre 2016

How to make it so that a scanner value will actually change the attribute for that value in a test code?

So I'm trying to make a very basic set of classes in Java that will allow me to make a circle and then change it's values and print out the new results including it's description, radius and area. I have the regular classes with the default and parameterized constructors here:

public class CircleArea
{
   public String description;
   public double radius;
   private double area;
   private double pi = 3.143;

   public CircleArea()
   {
      description = "Don't know";
      radius = -999.9;
   }

   public CircleArea(String _description, double _radius)
   {
      description = _description;
      radius = _radius;
   }

   public double calcArea()
   {
      area = pi * radius * radius;
      return area;
   }

}

And here is the Test class where I am trying to change the values for circle2:

import java.util.*;

public class TestCircleArea
{
  public static void main(String [] args)
 {
   CircleArea circle1 = new CircleArea();
   System.out.println("Description " + circle1.description + " has a radius of " + circle1.radius);

   CircleArea circle2 = new CircleArea("120-Circle", 10.0);
   System.out.println("Description " + circle2.description + " has a radius of " + circle2.radius + " and an area of " + circle2.calcArea()); 

   Scanner in = new Scanner(System.in);
   System.out.println("Enter circle description: ");
   String _description = in.nextLine();

   System.out.println("Enter circle radius: ");
   double _radius = in.nextDouble();

   System.out.println("Description " + circle2.description + " has a radius of " + circle2.radius + " and an area of " + circle2.calcArea());   
 }
}

My problem is that the third printout always just prints out the values that were in the parameterized constructor, not the values that are input into the scanner. How do I fix this?

I'm still very new to java, so this is probably a very simple mistake but it has me stumped. Thanks for taking the time to look at this question! Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire