Skip to content

Instantly share code, notes, and snippets.

@Bjacksonshorts
Last active April 26, 2022 18:23
Show Gist options
  • Save Bjacksonshorts/8660844 to your computer and use it in GitHub Desktop.
Save Bjacksonshorts/8660844 to your computer and use it in GitHub Desktop.
public class Circle {
protected int radius;
public Circle(int r) {
radius = r;
}
public void setRadius(int r) {
r = radius;
}
public int getRadius() {
return radius;
}
public int getDiameter() {
return radius * 2;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
}
public class Cylinder extends Circle{
protected int height;
public Cylinder(int r, int h){
super(r);
height = h;
}
public void setHeight(int h){
h = height;
}
public int getHeight(){
return height;
}
public double getVolume(){
return Math.PI * Math.pow(radius, 2) * height;
}
}
import java.util.Scanner;
public class Runner {
public static void main(String[] args) {
Cylinder c = new Cylinder(7,6);
Sphere s = new Sphere(6);
Scanner scanner = new Scanner (System.in);
System.out.print("What is this shape");
String shape = scanner.next(); // Get what the user types.{
if(shape.equalsIgnoreCase("Cylinder")){
System.out.println("the raidus of this Cylinder is " +c.getRadius());
System.out.println("the height of this Cylinder is " + c.getHeight());
System.out.println("the diamter of this Cylinder is " + c.getDiameter());
System.out.println("the Area of this Cylinder is " + c.getArea());
System.out.println("the volume of this Cylinder is " + c.getVolume());
}
else if (shape.equalsIgnoreCase("Sphere")){
System.out.println("the raidus of this Sphere is " +s.getRadius());
System.out.println("the volume of this Sphere is " + s.getVolume());
System.out.println("the diamter of this Sphere is " + s.getDiameter());
System.out.println("the Area of this Sphere is " + s.getArea());
}
else{
System.out.println("That shape is not applicable");
}
}
}
public class Sphere extends Circle {
public Sphere(int r){
super(r);
}
public double getVolume(){
return Math.PI * Math.pow(radius, 3) * 4/3;
}
public double getArea(){
return Math.PI * Math.pow(radius, 2) * 4;
}
}
@thatBrian
Copy link

Shouldn't setRadius be radius = r;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment