按照题目要求编写的圆,圆锥和测试类的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())
}
}
没那么多时间,帮着写个第1题吧
// 编写求一个整数数组A[10,15,12,9,7]中最小元素min和元素之和sum的
int [] a = {10,15,15,9,7}
// 最小元素
int min=0
// 数组和
int sum=0
for(int i=0 i<a.length i++ ){
sum += a[i]
if(i == 0){
min = a[i]
}else{
if(a[i] < min){
min = a[i]
}
}
}
System.out.println("当前数组中最小的元素值是: "+min)
System.out.println("当前数组和是: "+sum)
package TestPerson/**
* (1) 编写程序实现如下功能:已知Person类包含三个公共成员变量(姓名、性别、年龄)和一个构造方法,
* Student类是Person类的派生类,包含两个新的公共成员变量(学号、班号)、两个公共方法(修改年龄、显示基本信息)及一个构造方法。
* 在测试类Test1中,定义一组学生对象,并初始化他们的基本信息,然后依次输出。
*/
public class Test1 {
public static void main(String[] args) {
Student[] student = new Student[3]
student[0] = new Student("小李", "男", 12, 20181101, 01)
student[1] = new Student("小南", "女", 13, 20001102, 01)
student[2] = new Student("小李", "男", 12, 20181103, 01)
for(Student stu : student) {
stu.showInformation()
}
}
}
class Person {
public String name
public String sex
public int age
public Person(String name, String sex, int age) {
super()
this.name = name
this.sex = sex
this.age = age
}
}
class Student extends Person {
public long studentId
public long classId
public void setAge(int age) {
age = this.age
}
public void showInformation() {
System.out.println("我的姓名是" + name + "," + "我的性别是" + sex + "," + "我的年龄是" + age
+ "岁," + "我的学号是" + studentId + "," + "我的班号是" + classId + "班")
}
public Student(String name, String sex, int age, long studentId,
long classId) {
super(name, sex, age)
this.studentId = studentId
this.classId = classId
}
}
不可否认,我现在是有点闲,所以我就帮你写第一个吧,至于后面两个,我就不写了,看看还有没有其他人有点闲时间,看缘分吧
运行结果:
我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181101,我的班号是1班
我的姓名是小南,我的性别是女,我的年龄是13岁,我的学号是20001102,我的班号是1班
我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181103,我的班号是1班