2024-11-26 22:35:06
publicclassDay31{
publicstaticvoidmain(String[]args){
Animalch=newChicken("喔喔","芦花鸡","虫子","打鸣");
Animaldu=newChicken("嘎嘎","斑嘴鸭","小皮没鱼虾","游泳饥带");
AnimalAction.script(ch);
AnimalAction.script(du);
}
}
//父类
abstractclassAnimal{
protectedStringname,type,hobby,skill;;
protectedAnimal(Stringname,Stringtype,Stringhobby,Stringskill){
this.name=name;
this.type=type;
this.hobby=hobby;
this.skill=skill;
};
protectedvoidnameFun(){
System.out.println("我叫:"+name+"是一只"燃肢纳+type+"!");
}
protectedvoidhobbyFun(){
System.out.println("我喜欢吃:"+hobby+"!");
}
protectedvoidskillFun(){
System.out.println("我会:"+skill+"!");
}
}
//子类1鸡
classChickenextendsAnimal{
public?Chicken(Stringname,Stringtype,Stringhobby,Stringskill){
super(name,type,hobby,skill);
}
}
//子类2鸭
classDuckextendsAnimal{
publicDuck(Stringname,Stringtype,Stringhobby,Stringskill){
super(name,type,hobby,skill);
}
}
//调用类;
class?AnimalAction{
publicstaticvoidscript(Animalan){
an.nameFun();
an.hobbyFun();
an.skillFun();
}
}
有关java编程题目?按照题目要求源晌编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
classCircle{
privatedoubler;
privateStringcolor;
publicCircle(doubler){
this.r=r;
}
publicdoublearea(){
returnMath.PI*r*r;
}
publicdoubleperimeter(){
returnMath.PI*2*r;
}
publicdoublegetR(){
returnthis.r;
}
publicvoidsetR(doubler){
this.r=r;
}
publicStringgetColor(){
returnthis.color;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
publicStringtoString(){
return"圆的半径为"+r+",颜色为"+color;
}
}
classCone{
privateCirclec;
privatedoubleh;
privateStringcolor;
publicCone(Circlec,doubleh){
this.c=c;
this.h=h;
}
publicdoublevolume(){
return1.0/3*c.area()*h;
}
publicCirclegetCircle(){
returnthis.c;
}
publicvoidsetCircle(Circlec){
this.c=c;
}
publicdoublegetH(){
returnthis.h;
}
publicvoidsetH(doubleh){
this.h=h;
}
publicStringgetColor(){
returnthis.color;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
publicStringtoString(){
return"圆锥的底面积为"燃悔+c.area()+",高为"+h+",颜色为"+color;
}
}
publicclassTest{
publicstaticvoidmain(String[]args){
Circlecircle1=newCircle(2.5);
circle1.setColor("红色");
System.out.println(circle1.toString());
System.out.println("圆的面积为"+circle1.area());
System.out.println("圆的周雹段锋长为"+circle1.perimeter());
Conecirclar1=newCone(circle1,2.7);
circlar1.setColor("蓝色");
System.out.println(circlar1.toString());
System.out.println("圆锥的体积为"+circlar1.volume());
}
}
java练习题求完整代码
按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果
Student.java文件扮租代码如下
packageuser.java.test;
importjava.io.Serializable;
publicclassStudentimplementsSerializable{
privatestaticfinallongserialVersionUID=1L;
privateStringno;
privateStringname;
privatedoublescore;
publicStudent(){}
publicStudent(Stringno,Stringname,doublescore){
this.no=no;
this.name=name;
this.score=score;
}
publicStringgetNo(){returnno;}
publicvoidsetNo(Stringno){this.no=no;}
publicStringgetName(){returnname;}
publicvoidsetName(Stringname){this.name=name;}
publicdoublegetScore(){returnscore;}
publicvoidsetScore(doublescore){this.score=score;}
publicStringtoString(){
return"学号:"+no+",姓名:"+name+",成绩:"+score;
}
publicstaticdoublegetAvg(Student[]sArray){
doublesum=0,avg;
for(inti=0;isArray.length;i++){
sum=sum+sArray[i].getScore();
}
avg=sum/sArray.length;
returnavg;
}
}
Test.java文件代码如下
packageuser.java.test;
publicclassTest{
publicstaticvoidmain(String[]args){
Student[]sArray=newStudent[5];
sArray[0]=newStudent("001","张三",89.5);
sArray[1]=newStudent("002","李四",82.5);
sArray[2]=newStudent("003","王五",93);
sArray[3]=newStudent("004"皮缺和,"赵六",73.5);
sArray[4]=newStudent("005","燃盯孙七",66);
System.out.println("这些学生的平均分:"+Student.getAvg(sArray));
for(inti=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
java课程设计题目及代码是什么?java课程设计题目及代码分别是:
1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术芹此蔽运算。
设计要求是设计的计算器应用程序可以完成家法、减法、乘法、扒旦除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。
2、代码:
数字按钮NumberButton类如下:
importjava.awt.
importjava.awt.event.
importjavax.swing.
publicclassNumberButtonextendsButton.
{
intnumber.
publicNumberButton(intnumber).
?{
super(""+number).
this.number=number.
setForeground(Color.blue).
}
publicintgetNumber().
{
returnnumber;
}
}
其它java课程设计题目及代码是:
题目:华容道。编写一个按钮的子类,使用该子类创建的对象代表华容道中的人物。通过焦点事件控制人物颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。
通过键盘事件和鼠标事件来实现曹操、关羽等人物的移动。当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按下键盘的“↓“键,该人物向下移动。向左、向右和向上的移动原理类似。
代码是:
Stringname[]={"曹操","关羽","张","刘","嫌州马","许","兵","兵","兵","兵"}.
for(inti=0;iname.length;i++).
{
person[i]=newPerson(i,name[i]).
person[i].addKeyListener(this).
person[i].addMouseListener(this).
//???person[i].addFocusListener(newPerson).
add(person[i]).
}
person[0].setBounds(104,54,100,100).
person[1].setBounds(104,154,100,50).
person[2].setBounds(54,154,50,100).
person[3].setBounds(204,154,50,100).
person[4].setBounds(54,54,50,100).
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
几道JAVA题目,求好心人给下答案,感激不尽1、publicstaticvoidmain(String[]args)
2、publicfinallstatic
3、3
4、抽象类
5、implements
6、类:一个包含属性、方法,使用class关键字定义
??对象:创建对象必须使用new关键字,对象其实在内锋森存中存储的是类的引用地址。
7、try是可能发生异常的内容;
??catch是发生异常握宏后要处理的代码;
??finally是始终执行的,比如关闭资源等。
8、银皮亩
public?class?Demo?{
//小明今年12岁,他母亲比他大20岁。编程计算多少年后他母亲的年龄是小明的2倍。
public?static?void?main(String[]?args)?{
System.out.println(getYeah());
}
public?static?int?getYeah(){
int?xmAge?=?12,mqAge?=?20,yeah?=?0;
while(true){
if(mqAge?/?2?==?xmAge){
return?yeah;
}
mqAge++;
yeah++;
}
}
}
9、
public?class?Demo?{
public?static?void?main(String[]?args)?{
new?Thread(new?Piao()).start();
new?Thread(new?Piao()).start();
new?Thread(new?Piao()).start();
new?Thread(new?Piao()).start();
}
}
class?Piao?implements?Runnable{
private?static?int?piao?=?1;
public??void?run(){
while(true){
synchronized(Piao.class){
if(piao??100){
System.exit(0);
}
System.out.println(Thread.currentThread().getName()+"出票:"?+?piao?+?"号");
piao++;
}
}
}
}