Hi! this is a lengthy post/I hope its comprehensible
basically this program should calculate and display the area of a circle.....but it shows area as 0.0
for every value of radius entered.......I would really appreciate it if someone helps!
import java.util.Scanner;
public class circledriver
{
public static void main(String args[])
{
System.out.println("Welcome to Circle\n");
Scanner scan = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.print("Enter Circle ");
String select = sc.next();
sc.nextLine();
System.out.print("Enter Radius");
double Radius= scan.nextDouble();
sc.nextLine();
circleshape p = circleDB.getshape(select, Radius);
System.out.println();
if (p != null)
System.out.println(p.toString());
else
System.out.println("No CircleShape matches this selection.\n");
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}
This is a circledriver program......Basically the user has to select circle and enter the radius....then circleDB has to select the selection and create a circle object....
public class circleDB
{
public static circleshape getshape(String select,double Radius)
{
circleshape p = null;
if (select.equalsIgnoreCase("circle") )
{
circle c = new circle();
if (select.equalsIgnoreCase("circle"))
{
c.setSelection(select);
c.setRadius(Radius);
p = c;
}
}
return p;
}
}
now circleDB calls superclass circleshape where radius is defined and subclass circle which has area defined
import java.text.NumberFormat;
public class circleshape
{
private String Selection;
double Radius;
public static int count = 0;
public circleshape()
{
Selection= "";
Radius= 0;
}
public void setSelection(String Selection)
{
this.Selection = Selection;
}
public String getSelection()
{
return Selection;
}
public void setRadius(double Radius)
{
this.Radius = Radius;
}
public double getRadius()
{
return Radius;
}
public String toString()
{
return "Selection: " + Selection+ "\n" +
"Radius: " + Radius + "\n";
}
public static int getCount()
{
return count;
}
}
and this is the subclass circle
public class circle extends circleshape
{
double Area;
public circle()
{
super();
Area= 0.0;
count++;
}
public void setArea(double Area)
{
Radius=super.getRadius();
Area=3.14*Radius*Radius;
}
public double getArea(){
return Area;
}
public String toString()
{
return super.toString() +
"Area:" + Area;
}
}