《《类和对象》作业代码(18页).doc》由会员分享,可在线阅读,更多相关《《类和对象》作业代码(18页).doc(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-类和对象作业代码-第 18 页1、一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购 买的门票价格并输出package com.entity; /实体包import java.util.Scanner;public class Visitor public String name;public String age;public void show()Scanner input=new Scanner(System.in);while(!n.equals(name)System.out.print(请输入年龄:);int age=input.nextInt();if
2、(age50)System.out.println(name+的年龄是:+age+,门票免费);elseSystem.out.println(name+的年龄是:+age+,门票为20元);System.out.print(请输入姓名:);name=input.next();import java.util.Scanner; /测试包import com.entity.*;public class test2 public static void main(String args) Visitor visitor=new Visitor();Scanner input=new Scanner(
3、System.in);System.out.print(请输入姓名:);visitor.name=input.next();visitor.show(); System.out.println(退出程序!);2、 输入旧的用户名和密码,如果正确,方有权限更新;从键盘获取新的密码,进行更新import java.util.Scanner;public class Bank public String user=; public int password=0; public void show() Scanner input=new Scanner(System.in);for(;)System.
4、out.println(请输入用户名:);String user1=input.next();System.out.println(请输入密码:);int password1=input.nextInt();if(user1.equals(bank1.user) & password1=bank1.password) System.out.println(登录成功,可以做修改了); System.out.println(请输入新用户名:); String newname=input.next(); System.out.println(请输入新密码:); int newpassword=inp
5、ut.nextInt(); System.out.println(新用户名是:+newname+,新密码是: +newpassword); break;elseSystem.out.println(用户名或密码错误,请重新登录);import com.*;public class Bank2 public static void main(String args)Bank bank1=new Bank();bank1.user=admin1;bank1.password=111111;bank1.show();3、 小明过生日,爸爸送他一个电动狮子玩具,编程测试这个狮子能否正常工作 packa
6、ge com.entity1; public class Xiyouji public String color=黄色;public String run() return正在疯狂地跑,;public void sound()System.out.println(并且唱着歌);public String getcolor()return color;public void showLion()System.out.print(这是一只+getcolor()+的小狮子,);import .*;public class Xiyouji1 public static void main(String
7、args)Xiyouji lion=new Xiyouji();lion.showLion();System.out.print(lion.run();lion.sound();4、从键盘接收三门课分数,计算三门课的平均分和总成绩,编写成绩计算类实现功能package com.entity1;import java.util.Scanner;public class Xiyouji Scanner input=new Scanner(System.in);public int sum;public double avg;public void score()System.out.print(请
8、输入java成绩:);int java_score=input.nextInt();System.out.print(C#成绩:);int c_score=input.nextInt();System.out.print(请输入DB成绩:);int DB_score=input.nextInt();sum=java_score+c_score+DB_score;avg=sum/3;public void showScore()System.out.print(总成绩是:+sum+,平均分是:+avg);import .*;public class Xiyouji1 public static
9、void main(Stringargs) Xiyouji score1=new Xiyouji(); score1.score(); score1.showScore();5、编写手机类(Phone):可以下载音乐,可以播放这些音乐,可以进行充电package com.entity1;public class Xiyouji public String brand=华为;public String downloadMusic() return安静-周杰伦;public String playMusic()return同时播放着下载的音乐:+downloadMusic();public voi
10、d charge()System.out.print(正在充电,);import .*;public class Xiyouji1 public static void main(Stringargs) Xiyouji phone=new Xiyouji(); phone.brand=华为手机; System.out.print(phone.brand);/ phone.brandPhone(); phone.charge(); System.out.print(phone.playMusic();6、在实现了增加一个学生姓名的基础上,增加学生的学号、年龄和成绩,并显示这些信息,如何实现?pa
11、ckage com.entity1;public class Student public int number;public String name;public int age;public int score;static Student students=new Student 2;/static意思是静态,可以修饰类、字段、属性、方法,标记为static的就不用创建实例对象调用 public static void show()for(int i=0;i2;i+)System.out.println(studentsi.number+t+studentsi.name+t+studen
12、tsi.age+t+studentsi.score);public void addStudent(Student stu)for(int i=0;istudents.length;i+)if(studentsi=null)studentsi=stu;break;package test;import com.entity1.Student;public class StudentTest public static void main(String args) Student student1=new Student();student1.number=1000;student1.name=
13、张三;student1.age=22;student1.score=80;Student student2=new Student();student2.number=1002;student2.name=张二;student2.age=21;student2.score=75;Student student=new Student();student.addStudent(student1);student.addStudent(student2);student.show();7、注册新用户,要求密码长度不能小于6位import java.util.*;public class Chars
14、et public static void main(String args) Scanner input=new Scanner(System.in);System.out.print(请输入用户名:);String username=input.next();System.out.print(请输入密码:);String password=input.next();if(password.length()6) flag=true; return flag;import com.entity1.*;import java.util.Scanner;public class charsetTe
15、st public static void main(String args) /注册Scanner input=new Scanner(System.in);System.out.print(请输入用户名:);String username1=input.next();System.out.print(请输入密码:);String password1=input.next();Charset charset=new Charset();boolean flag=charset.register(password1);if(password1.length()=6)System.out.pri
16、ntln(注册成功);elseSystem.out.println(密码的长度不能小于6);/登录验证System.out.print(请输入用户名:);String username2=input.next();System.out.print(请输入密码:);String password2=input.next();/if(username2.equalsIgnoreCase(username2) & (password2.equalsIgnoreCase(password1)if(username2.toLowerCase().equals(username2).toLowerCase
17、() & (password2.toUpperCase().equals(password1).toUpperCase()System.out.println(登录成功!);elseSystem.out.println(用户名或密码错误,登录失败!);8、判断.java文件名是否正确,判断邮箱格式是否正确import java.util.*;public class CharsetTest4 public static void main(String args)System.out.println(-欢迎进入作业提交系统-);Scanner input=new Scanner(System.
18、in);System.out.print(请输入java文件名:);String name=input.next();String str=name.substring(name.indexOf(.)+1,name.length();if(name.indexOf(.)!=0 & name.indexOf(.)!=-1 & str.equals(java) System.out.println(有效java文件);elseSystem.out.println(无效java文件);System.out.print(请输入你的邮箱:);String email=input.next();if(em
19、ail.indexOf()!=-1 & email.indexOf(.)!=-1 & email.indexOf(.)email.indexOf()System.out.println(作业提交成功);elseSystem.out.println(java文件或email无效,提交失败);9、 有一段歌词,每句都以空格“ ”结尾,请将歌词每句按行输出public class CharsetTest5 public static void main(String args) String words=长亭外,古道边,芳草碧连天,晚风扶,柳笛声残,夕阳山外山;String word=new Str
20、ing 100;System.out.println(原格式:n+words);System.out.println(拆分后的格式:);word=words.split(,);for (String s1 : word) System.out.println(s1+t);/for(int i=0;iword.length;i+)/System.out.println(wordi);10、import java.util.*;public class DVDMgr Scanner input=new Scanner(System.in);DvdSet dvd=new DvdSet 5;publi
21、c void initial()dvd0=new DvdSet();dvd0.name=罗马度假;dvd0.state=0;dvd0.date=2013-7-1;dvd1=new DvdSet();dvd1.name=风声鹤唳;dvd1.state=1;dvd1.date=;dvd2=new DvdSet();dvd2.name=浪漫满屋;dvd2.state=1;dvd2.date=;public void startMenu() /菜单切换boolean flag=false;doint choice=0;System.out.println(1.新增DVD);System.out.pri
22、ntln(2.查看DVD);System.out.println(3.删除DVD);System.out.println(4.借出DVD);System.out.println(5.归还DVD);System.out.println(6.退 出);System.out.print(请选择:);boolean flag1=input.hasNextInt();if(flag1) choice=input.nextInt();switch(choice)case 1:System.out.println(此处实现新增DVD);System.out.println(*);break;case 2:S
23、ystem.out.println(此处实现查看DVD);System.out.println(*);break;case 3:System.out.println(此处实现删除DVD);System.out.println(*);break;case 4:System.out.println(此处实现借出DVD);System.out.println(*);break;case 5:System.out.println(此处实现归还DVD);System.out.println(*);break;default:System.out.println(输入错误,请重新输入:);flag=tru
24、e; break;elseSystem.out.println(输入错误,请重新输入:);flag1=true;continue;/System.out.print(n输入0返回主菜单:);/int num=input.nextInt();/if(num=0)/continue;/else/System.out.println(输入错误,请重新输入0:);/flag=true;while(flag);public void showDvd() System.out.println(t序号t状态t借出日期); int count=1; for(int i=0;idvd.length;i+) Sy
25、stem.out.println(count+t+dvdi.state +t+dvdi.name+t+dvdi.date); count+; System.out.println(*);public class DvdSet String name;int state;String date;import java.util.*;import com.entity1.*;public class Start public static void main(String args) DVDMgr mgr=new DVDMgr();Scanner input=new Scanner(System.
26、in); System.out.println(欢迎使用迷你DVD管理器); System.out.println(-); mgr.startMenu();11、 某汽车租赁公司出租多种车辆,车型及租金情况如下:轿车客车(金杯、金龙)车型别克商务舱GL8宝马550i别克林荫大道16座日租费(元/天)6005003008001500public abstract class MotoVehicle /父类protected String no=null;private String brand=null;private String color=null;private String milea
27、ge=null;int sum;public abstract int Calcrent(int days); /抽象方法public class Car extends MotoVehicleprivate int type; /属性public Car(String NO, int type) /构造方法this.no=no;this.type=type;public int Calcrent(int days) /方法重写return 0;public int Calcrent(int days,int type) /方法重载switch(type)case 1:sum=600*days
28、;break;case 2:sum=500*days;break;case 3:sum=300*days;break;System.out.println(租赁费用为:+sum+元);return sum;public final class Bus extends MotoVehicleprivate int seatcount; /属性public Bus(String no, int seatcount) /构造方法this.no=no;this.seatcount = seatcount;public int Calcrent(int days) /方法重写,实现父类中的抽象方法ret
29、urn 0;public int Calcrent(int days,int seatcount) /方法重载if(seatcount16)sum=1500*days; System.out.println(费用为:+sum+元); return sum;import com.entity1.Bus;import com.entity1.Car;public class TestMotoVehicle public static void main(String args)System.out.println(欢迎来到汽车租赁公司);Scanner input=new Scanner(Syst
30、em.in);System.out.print(请输入要租赁的汽车类型(1.轿车 2.客车):);int number=input.nextInt();if(number=1)System.out.print(请输入轿车品牌(1.别克商务舱GL8 2.宝马550i 3.别克林荫大道):);int num=input.nextInt();System.out.println(请输入要租赁的天数:);int days=input.nextInt();String no=京K6866;Car car=new Car(no,num); /调用Car类System.out.println(给你分配的车牌号是+no);car.Calcrent(days,num);else if(number=2)System.out.print(请输入座位数:);int seat=input.nextInt();System.out.println(请输入要租赁的天数:);int days=input.nextInt();String no=京K6866;Bus bus=new Bus(no,seat); /调用Bus类System.out.println(给你分配的车牌号是+no);bus.Calcrent(days,seat);