class
student{
private
string
stuid
private
string
stuname
private
char
stusex
private
int
stuage
/**
*以下是各属性的getter/setter方法
*/
public
void
setstuid(string
stuid){
this.stuid=stuid
}
public
string
getstuid(){
return
stuid
}
public
void
setstuname(string
stuname){
this.stuname=stuname
}
public
string
getstuname(){
return
stuname
}
public
void
setstusex(char
stusex){
this.stusex=stusex
}
public
char
getstusex(){
return
stusex
}
public
void
setstuage(int
stuage){
this.stuage=stuage
}
public
int
getstuage(){
return
stuage
}
/**
*构造方法,构造学生信息
*/
public
student(string
stuid,string
stuname,char
stusex,int
stuage){
this.stuid=stuid
this.stuname=stuname
this.stusex=stusex
this.stuage=stuage
}
public
string
tostring(){//覆盖该类的tostring()方法
stringbuffer
buff=new
stringbuffer()
buff.append("学号:"+stuid)
buff.append("\n姓名:"+stuname)
buff.append("\n性别:"+stusex)
buff.append("\n年龄:"+stuage)
return
buff.tostring()
}
public
static
void
main(string[]
args){
student
stu=new
student("1000","zhangsan",'男',18)
system.out.println
(stu)//打印学生信息
system.out.println
("--修改姓名结果--")
stu.setstuname("lisi")
system.out.println
(stu)
}
}
按照题目要求编写的圆,圆锥和测试类的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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)