Last active
April 26, 2022 18:23
-
-
Save Bjacksonshorts/8660844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't setRadius be radius = r;