按照题目要求编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
class Circle{
private double r
private String color
public Circle(double r){
this.r=r
}
public double area(){
return Math.PI*r*r
}
public double perimeter(){
return Math.PI*2*r
}
public double getR(){
return this.r
}
public void setR(double r){
this.r=r
}
public String getColor(){
return this.color
}
public void setColor(String color){
this.color=color
}
public String toString(){
return "圆的半径为"+r+",颜色为"+color
}
}
class Cone{
private Circle c
private double h
private String color
public Cone(Circle c,double h){
this.c=c
this.h=h
}
public double volume(){
return 1.0/3*c.area()*h
}
public Circle getCircle(){
return this.c
}
public void setCircle(Circle c){
this.c=c
}
public double getH(){
return this.h
}
public void setH(double h){
this.h=h
}
public String getColor(){
return this.color
}
public void setColor(String color){
this.color=color
}
public String toString(){
return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5)
circle1.setColor("红色")
System.out.println(circle1.toString())
System.out.println("圆的面积为"+circle1.area())
System.out.println("圆的周长为"+circle1.perimeter())
Cone circlar1=new Cone(circle1,2.7)
circlar1.setColor("蓝色")
System.out.println(circlar1.toString())
System.out.println("圆锥的体积为"+circlar1.volume())
}
}
public class Person {private String name//姓名
private String sex//性别
public void sayHello() {
System.out.println("姓名:" + name)
System.out.println("性别:" + sex)
}
public Person() {
}
public Person(String name, String sex) {
this.name = name
this.sex = sex
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public String getSex() {
return sex
}
public void setSex(String sex) {
this.sex = sex
}
}
public class Student extends Person {
private String num//学号
private String school//学校
public void sayHello() {
super.sayHello()
System.out.println("学号:" + num)
System.out.println("学校:" + school)
}
public Student(String num, String school) {
this.num = num
this.school = school
}
public Student(String name, String sex, String num, String school) {
super(name, sex)
this.num = num
this.school = school
}
public Student() {
}
public String getNum() {
return num
}
public void setNum(String num) {
this.num = num
}
public String getSchool() {
return school
}
public void setSchool(String school) {
this.school = school
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student()
stu1.setName("张三")
stu1.setSex("男")
stu1.setNum("20211225001")
stu1.setSchool("北京大学")
Student stu2 = new Student("20211225002", "北京大学")
stu2.setName("李四")
stu2.setSex("男")
Student stu3 = new Student("王五", "女", "20211225003", "清华大学")
Person person1 = new Person()
person1.setName("赵六")
person1.setSex("女")
Person person2 = new Person("孙七", "女")
stu1.sayHello()
stu2.sayHello()
stu3.sayHello()
person1.sayHello()
person2.sayHello()
}
}
import java.io.BufferedInputStreamimport java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
public class Test {
public static void main(String[] args) {
BufferedOutputStream out = null
try {
out = new BufferedOutputStream(new FileOutputStream(new File("d:/info.txt")))
String line = "第一行文本\n第二行文本"
out.write(line.getBytes())
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (out != null) {
try {
out.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
BufferedInputStream in = null
try {
in = new BufferedInputStream(new FileInputStream("d:/info.txt"))
StringBuffer buffer = new StringBuffer()
byte[] buff = new byte[in.available()]
while (in.read(buff) != -1) {
buffer.append(new String(buff))
}
System.out.println(buffer)
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (in != null) {
try {
in.close()
in = null
} catch (IOException e) {
e.printStackTrace()
}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)