您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业财务 > 《类和对象》作业代码
1、一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购买的门票价格并输出packagecom.entity;//实体包importjava.util.Scanner;publicclassVisitor{publicStringname;publicStringage;publicvoidshow(){Scannerinput=newScanner(System.in);while(!n.equals(name)){System.out.print(请输入年龄:);intage=input.nextInt();if(age10||age50){System.out.println(name+的年龄是:+age+,门票免费);}else{System.out.println(name+的年龄是:+age+,门票为20元);}System.out.print(请输入姓名:);name=input.next();}}}importjava.util.Scanner;//测试包importcom.entity.*;publicclasstest2{publicstaticvoidmain(String[]args){Visitorvisitor=newVisitor();Scannerinput=newScanner(System.in);System.out.print(请输入姓名:);visitor.name=input.next();visitor.show();System.out.println(退出程序!);}}2、输入旧的用户名和密码,如果正确,方有权限更新;从键盘获取新的密码,进行更新importjava.util.Scanner;publicclassBank{publicStringuser=;publicintpassword=0;publicvoidshow(){Scannerinput=newScanner(System.in);for(;;){System.out.println(请输入用户名:);Stringuser1=input.next();System.out.println(请输入密码:);intpassword1=input.nextInt();if(user1.equals(bank1.user)&&password1==bank1.password){System.out.println(登录成功,可以做修改了);System.out.println(请输入新用户名:);Stringnewname=input.next();System.out.println(请输入新密码:);intnewpassword=input.nextInt();System.out.println(新用户名是:+newname+,新密码是:+newpassword);break;}else{System.out.println(用户名或密码错误,请重新登录);}}importcom.*;publicclassBank2{publicstaticvoidmain(String[]args){Bankbank1=newBank();bank1.user=admin1;bank1.password=111111;bank1.show();}}3、小明过生日,爸爸送他一个电动狮子玩具,编程测试这个狮子能否正常工作packagecom.entity1;publicclassXiyouji{publicStringcolor=黄色;publicStringrun(){return正在疯狂地跑,;}publicvoidsound(){System.out.println(并且唱着歌);}publicStringgetcolor(){returncolor;}publicvoidshowLion(){System.out.print(这是一只+getcolor()+的小狮子,);}}importcom.entity1.*;publicclassXiyouji1{publicstaticvoidmain(String[]args){Xiyoujilion=newXiyouji();lion.showLion();System.out.print(lion.run());lion.sound();}}4、从键盘接收三门课分数,计算三门课的平均分和总成绩,编写成绩计算类实现功能packagecom.entity1;importjava.util.Scanner;publicclassXiyouji{Scannerinput=newScanner(System.in);publicintsum;publicdoubleavg;publicvoidscore(){System.out.print(请输入java成绩:);intjava_score=input.nextInt();System.out.print(C#成绩:);intc_score=input.nextInt();System.out.print(请输入DB成绩:);intDB_score=input.nextInt();sum=java_score+c_score+DB_score;avg=sum/3;}publicvoidshowScore(){System.out.print(总成绩是:+sum+,平均分是:+avg);}}importcom.entity1.*;publicclassXiyouji1{publicstaticvoidmain(String[]args){Xiyoujiscore1=newXiyouji();score1.score();score1.showScore();}}5、编写手机类(Phone):可以下载音乐,可以播放这些音乐,可以进行充电packagecom.entity1;publicclassXiyouji{publicStringbrand=华为;publicStringdownloadMusic(){return安静-周杰伦;}publicStringplayMusic(){return同时播放着下载的音乐:+downloadMusic();}publicvoidcharge(){System.out.print(正在充电,);}}importcom.entity1.*;publicclassXiyouji1{publicstaticvoidmain(String[]args){Xiyoujiphone=newXiyouji();phone.brand=华为手机;System.out.print(phone.brand);//phone.brandPhone();phone.charge();System.out.print(phone.playMusic());}}6、在实现了增加一个学生姓名的基础上,增加学生的学号、年龄和成绩,并显示这些信息,如何实现?packagecom.entity1;publicclassStudent{publicintnumber;publicStringname;publicintage;publicintscore;staticStudent[]students=newStudent[2];//static意思是静态,可以修饰类、字段、属性、方法,标记为static的就不用创建实例对象调用publicstaticvoidshow(){for(inti=0;i2;i++){System.out.println(students[i].number+\t+students[i].name+\t+students[i].age+\t+students[i].score);}}publicvoidaddStudent(Studentstu){for(inti=0;istudents.length;i++){if(students[i]==null){students[i]=stu;break;}}}}packagetest;importcom.entity1.Student;publicclassStudentTest{publicstaticvoidmain(String[]args){Studentstudent1=newStudent();student1.number=1000;student1.name=张三;student1.age=22;student1.score=80;Studentstudent2=newStudent();student2.number=1002;student2.name=张二;student2.age=21;student2.score=75;Studentstudent=newStudent();student.addStudent(student1);student.addStudent(student2);student.show();}}7、注册新用户,要求密码长度不能小于6位importjava.util.*;publicclassCharset{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);System.out.print(请输入用户名:);Stringusername=input.next();System.out.print(请输入密码:);Stringpassword=input.next();if(password.length()6){System.out.println(密码长度不能小于6);}else{System.out.println(注册成功);}}}7、注册新用户,要求密码长度不能小于6位,注册成功后,实现登录验证。packagecom.entity1;importjava.util.*;publicclassCharset{publicStringusername;publicStringpassword;publicbooleanregister(Stringpassword){booleanflag=false;if(password.length()6){flag=true;}returnflag;}}importcom.entity1.*;importjava.util.Scanner;publicclasscharsetTest{publicstaticvoidmain(String[]args){//注册Scannerinput=newScanner(System.in);System.out.print(请输入用户名:);Stringusername1=input.next();System.out.print(请输入密码:);Stringpassword1=input.next();Charsetcharset=newCharset();booleanflag=charset.register(password1);if(password1.length()=6){System.out.println(注册成功);}else{System.out.println(密码的长度不能小于6);}//登录验证System.out.print(请输入用户名:);Stringusername2=input.next();System.out.print(请输入密码:);Stringpassword2=input.next();//if(username2.equalsIgnoreCase(username2)&&(password2.equalsIgnoreCase(password1))){if(username2.toLowerCase().equals((userna
本文标题:《类和对象》作业代码
链接地址:https://www.777doc.com/doc-2801225 .html