//I cannot figure out why my code is 1) giving me a null output 2) not calculating the max, min values, and average values. Any help? There are two classes in my code, and I'm supposed to find the average, max, and min of a set of values. -->
public class CirclesV8
{
//declaration of private instance variables
private double myRadius, mySphere;
private double myArea = 0, total = 0;
//instance variables for totals average
private double MyTotal = 0;
private double MyAverage = 0;
//constructor for objects of type CirclesV8
public CirclesV8(int r, int s)
{
myRadius = r;
mySphere = s;
MyTotal = total;
}
//getter method for radius and total
public double getRadius()
{
return myRadius;
}
public double getSphere()
{
return mySphere;
}
public double getTotal()
{
return MyTotal;
}
//mutator method for the area, average, and circumference
public void calcArea()
{
myArea = Math.PI * (Math.pow(myRadius, 2));
}
public double getArea()
{
return myArea;
}
public void calcSurfaceArea()
{
mySphere = (Math.pow(myRadius, 2)) * Math.PI * 4;
}
public double getSurfaceArea()
{
return mySphere;
}
public void calcAvg()
{
MyAverage = MyTotal / 5;
}
public double getAvg()
{
return MyAverage;
}
//setter method for myTotal
public void setTotal(double total)
{
MyTotal = total;
}
// returns a String of the object's values. The format() method is similar to printf().
public String toString()
{
return String.format("%12d %14.1f %13.1f 12.1f", myRadius,
myArea,
mySphere,
MyAverage);
}
}
import java.util.Scanner;
public class V8Tester
{
public static void main(String[] args)
{
//create scanner object
Scanner in = new Scanner(System.in);
//initialize variables
int radius1, radius2, radius3, radius4, radius5;
int sphere1, sphere2, sphere3, sphere4, sphere5;
double Average = 0;
double total = 0;
double min = Integer.MAX_VALUE;
double max = Integer.MIN_VALUE;
System.out.println("Please enter five radiuses separate by space (ex. 6 12)");
radius1 = in.nextInt();
radius2 = in.nextInt();
radius3 = in.nextInt();
radius4 = in.nextInt();
radius5 = in.nextInt();
System.out.println("Please enter another 5 radiuses separate by space (ex. 6 12)");
sphere1 = in.nextInt();
sphere2 = in.nextInt();
sphere3 = in.nextInt();
sphere4 = in.nextInt();
sphere5 = in.nextInt();
//Create array of objects
CirclesV8 [] circles = {new CirclesV8(radius1, sphere1),
new CirclesV8(radius2, sphere2),
new CirclesV8(radius3, sphere3),
new CirclesV8(radius4, sphere4),
new CirclesV8(radius5, sphere5)};
//call methods
for(int index = 0; index < circles.length; index++)
{
circles[index].calcArea();
circles[index].calcSurfaceArea();
circles[index].calcAvg();
//store totals for calculating average
total += circles[index].getTotal();
//calc max and min
if(circles[index].getTotal() < min)
min = circles[index].getTotal();
if(circles[index].getTotal() > max)
max = circles[index].getTotal();
}
//set total values & call average methods to calculate averages using one of the objects
circles[4].setTotal(total);
circles[4].calcArea();
circles[4].calcSurfaceArea();
circles[4].getAvg();
//Circle Colors
System.out.println("Color of Circle #1: ");
String circle1 = in.next();
System.out.println("Color of Circle #2: ");
String circle2 = in.next();
System.out.println("Color of Circle #3: ");
String circle3 = in.next();
System.out.println("Color of Circle #4: ");
String circle4 = in.next();
System.out.println("Color of Circle #5: ");
String circle55 = in.next();
String[] array = new String[5];
array[0] = circle1;
array[1] = circle2;
array[2] = circle3;
array[3] = circle4;
array[4] = circle55;
//Sphere Colors
System.out.println("Color of Sphere #1: ");
String sphere11 = in.next();
System.out.println("Color of Sphere #2: ");
String sphere22 = in.next();
System.out.println("Color of Sphere #3: ");
String sphere33 = in.next();
System.out.println("Color of Sphere #4: ");
String sphere44 = in.next();
System.out.println("Color of Sphere #5: ");
String sphere55 = in.next();
String[] arr = new String[5];
array[0] = sphere11;
array[1] = sphere22;
array[2] = sphere33;
array[3] = sphere44;
array[4] = sphere55;
int i = 1;
System.out.println(" Color Radius Surface Area Area");
System.out.println("==============================================================");
for(int index = 0; index < circles.length; index++)
{
System.out.printf("%4s %10.1f %14.1f%n", array[index],
circles[index].getRadius(),
circles[index].getArea(),
i++);
}
public class CirclesV8
{
//declaration of private instance variables
private double myRadius, mySphere;
private double myArea = 0, total = 0;
//instance variables for totals average
private double MyTotal = 0;
private double MyAverage = 0;
//constructor for objects of type CirclesV8
public CirclesV8(int r, int s)
{
myRadius = r;
mySphere = s;
MyTotal = total;
}
//getter method for radius and total
public double getRadius()
{
return myRadius;
}
public double getSphere()
{
return mySphere;
}
public double getTotal()
{
return MyTotal;
}
//mutator method for area, average, and circumference
public void calcArea()
{
myArea = Math.PI * (Math.pow(myRadius, 2));
}
public double getArea()
{
return myArea;
}
public void calcSurfaceArea()
{
mySphere = (Math.pow(myRadius, 2)) * Math.PI * 4;
}
public double getSurfaceArea()
{
return mySphere;
}
public void calcAvg()
{
MyAverage = MyTotal / 5;
}
public double getAvg()
{
return MyAverage;
}
//setter method for myTotal
public void setTotal(double total)
{
MyTotal = total;
}
// returns a String of the object's values. The format() method is similar to printf().
public String toString()
{
return String.format("%12d %14.1f %13.1f 12.1f", myRadius,
myArea,
mySphere,
MyAverage);
}
}
this gives me a null output
for(int index = 0; index < circles.length; index++)
{
System.out.printf("%4s %10.1f %20f", arr[index],
circles[index].getRadius(),
circles[index].getSurfaceArea(),
i++);
}
System.out.println("==============================================================");
I can't get this to print values
System.out.printf("%20s%n", "Minimum: ", max);
System.out.printf("%20s%n", "Maximum: ", min);
System.out.printf("%20s%n", "Average: ", circles[4].getAvg());
}
}
Aucun commentaire:
Enregistrer un commentaire