书写的练习代码
- package com.kgc.demo;
//成绩类
public class Score {
private int javaScore;
private int CScore;
public int getJavaScore() {
return javaScore;
}
public void setJavaScore(int javaScore) {
this.javaScore = javaScore;
}
public int getCScore() {
return CScore;
}
public void setCScore(int cScore) {
CScore = cScore;
}
//构造方法
public Score(int javaScore, int cScore) {
super();
this.javaScore = javaScore;
CScore = cScore;
}
public Score() {
super();
}
} - package com.kgc.demo;
/**
- 封装一个学生类javaBean,包含String name、int age属性。
封装一个成绩类javaBean,包含int javaScore、int CScore属性。
使用泛型实例化ArrayList集合,此集合只能存放成绩类对象。
使用泛型实例化HashMap<学生,成绩>。
使用泛型实例化HashSet集合对象,要求只能存放成绩类对象。
从控制台输入如下信息:( 姓名|年龄|Java成绩|C语言成绩 )
郭靖|23|90|95
黄蓉|18|97|88
夏娃|20|95|92
亚当|17|93|91
用控制台输入的信息,实例化学生类对象和成绩类对象。
将成绩对象放入ArrayList集合中,按照C语言成绩对集合排序,由低到高正序输出。
(要求使用ListIterator迭代器倒序进行遍历)
将学生和成绩放入HashMap集合,由于黄蓉找到教务要求加分,故将黄蓉的C语言成绩改为78分。
改分后要求使用迭代器遍历输出HashMap,格式为:姓名-年龄-java成绩-c语言成绩。
将HashMap集合转换为Set集合(集合中存储Map的键值对),并遍历输出。
将成绩对象放入HashSet集合,然后遍历集合,要求使用迭代器进行遍历。
*/
//学生类
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//构造方法
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
}
- package com.kgc.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
List list=new ArrayList();
Score one=new Score(98,76);
Score one1=new Score(95,86);
Score one2=new Score(91,78);
Score one3=new Score(93,69);
list.add(one);
list.add(one1);
list.add(one2);
list.add(one3);
HashMap <Student, Score> lists=new HashMap<Student, Score>();
Student ones=new Student(“郭靖”, 23);
Student ones1=new Student(“黄蓉”, 18);
Student ones2=new Student(“夏娃”, 20);
Student ones3=new Student(“亚当”, 17);
lists.put(ones, one);
lists.put(ones1, one1);
lists.put(ones2, one2);
lists.put(ones3, one3);
Set keySet = lists.keySet();
for (Student stu : keySet) {
System.out.println(stu.getName()+"|"+stu.getAge()+"|"+lists.get(stu).getJavaScore()+"|"+lists.get(stu).getCScore());
}
for (Student stu : keySet) {
if(stu.getName().equals(“黄蓉”)){
lists.get(stu).setCScore(78);
}
System.out.println(stu.getName()+"|"+stu.getAge()+"|"+lists.get(stu).getJavaScore()+"|"+lists.get(stu).getCScore());
}
}
}s
4. package com.kgc.demo1;
//汽车大巴类
public class Bus extends Car{
//定义bus的特有方法
private int namber;
private int hao;
private String goShi;
//set get方法
public int getNamber() {
return namber;
}
public void setNamber(int namber) {
this.namber = namber;
}
public int getHao() {
return hao;
}
public void setHao(int hao) {
this.hao = hao;
}
public String getGoShi() {
return goShi;
}
public void setGoShi(String goShi) {
this.goShi = goShi;
}
//构造方法
public Bus(int namber, int hao, String goShi,int ren) {
super(ren);
this.namber = namber;
this.hao = hao;
this.goShi = goShi;
}
//继承父类的方法
public void last() {
System.out.println("我载了"+super.getRen()+"个人哦!");
}
public void shoInfo() {
System.out.println("我是"+this.hao+"大巴车,"+"是"+this.goShi+"公司的,"+"我能载:"+super.getRen()+"个人哦!");
System.out.println("北京到了!");
}
@Override
public String toString() {
return namber + "," + hao + ", " + goShi
+ ", " + getRen();
}
}
-
package com.kgc.demo1;
//轿车类
public class byCar extends Car{
//轿车特有的属性
private String type;
private String color;
//构造方法public byCar(String type, String color,int ren) {
super(ren);
this.type = type;
this.color = color;
}
//set get方法
public String getType() {
return type;
}public void setType(String type) {
this.type = type;
}public String getColor() {
return color;
}public void setColor(String color) {
this.color = color;
}//重新父类方法
public void last() {
System.out.println(“我载了”+super.getRen()+“个人哦!”);
}public void shoInfo() {
System.out.println(“我是”+this.type+“车,”+“是”+this.color+“颜色的”);
}
@Override
public String toString() {
return type + ", " + color + ", "
+ getRen() ;
}
}
-
package com.kgc.demo1;
//车类
public abstract class Car {
//车公有的属性
private int ren;
//set get 方法
public int getRen() {
return ren;
}
public void setRen(int ren) {
this.ren = ren;
}
//构造方法
public Car(int ren) {
super();
this.ren = ren;
}
//定义车载人的方法
public abstract void last();
//定义车自我介绍的功能
public abstract void shoInfo();
} -
package com.kgc.demo1;
public class test {
public static void main(String[] args) {
//购进车辆
Car car[]=new Car[4];
car[0]=new Bus(2578, 156, “宇通”, 30);
car[1]=new Bus(2348, 198556, “宇通”, 30);
car[2]=new byCar(“奥迪”, “红”, 4);
car[3]=new byCar(“宝马”, “黄”, 4);
System.out.println(“欢迎您的到来!”);
for(Car i:car){
System.out.println(i);
//两种强制转换方式
if(i instanceof byCar){
((byCar)i).shoInfo();
((byCar)i).last();
}else{
Bus k=(Bus)i;
k.shoInfo();
k.last();
}
}
}
}
- package com.kgc.demo2;
/**在实际开发中,我们往往需要将一个 int类型的毫秒数转换成对应的 时间(小时分钟秒)
- 。要求输入一个整型作为毫秒数,得出对应的 小时,分钟,秒。如果不够一分钟,则显示格式如 x秒,
- 如果不够一小时,则显示格式如 x分x秒,如果超过一小时,则显示格式如 x小时x分x秒。
3、案例完成思路要求:
A. 定义一个类,类中定义一个方法实现上述需求,要求参数为 int类型(毫秒数)) (10分)
B. 正确转换成小时、分钟、秒(15分)
C. 调用该方法,并打印如图效果 (10分)
- */
public abstract class Demo1 {
//属性变量
private int time;
//setget方法
public int getTime() {
return time;
}
public void setTime(int time) {
if(time>0){
this.time = time;
}else{
try {
throw new Exception(“输入有误!”);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public abstract void lest();
}
- package com.kgc.demo2;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Demo1 one=new TestDemo1();
System.out.println(“请输入时间!”);
Scanner input =new Scanner (System.in);
int time=input.nextInt();
one.setTime(time);
one.lest();
}
}
- package com.kgc.demo2;
public class TestDemo1 extends Demo1 {
//重写父类方法
public void lest() {
if(super.getTime()>0&&super.getTime()<60){
System.out.println(“您运动了”+super.getTime()+“秒!”);
}else if(super.getTime()>=60&&super.getTime()<3600){
System.out.println(“您运动了”+super.getTime()/60+“分”+super.getTime()%60+“秒!”);
}else if(super.getTime()>=3600){
System.out.println(“您运动了”+super.getTime()/3600+“时!”+super.getTime()/60%60+“分”+super.getTime()%60+“秒!”);
}
}
}
- package com.kgc.wook;
public abstract class Reng {
/**8.定义一个人类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉,工作。
(1)根据人类,派生一个学生类,增加属性:学校、学号;重写工作方法(学生的工作是学习)。
(2)根据人类,派生一个工人类,增加属性:单位、工龄;重写工作方法(工人的工作是……自己想吧)。
(3)根据学生类,派生一个学生干部类,增加属性:职务;增加方法:开会。
(4)编写主函数分别对上述3类具体人物进行测试。
*/
//人的属性
private String name;//名字
private String sex;//性别
private int age;//年龄
private String nationality;//国籍
//set get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
//构造方法
public Reng(String name, String sex, int age, String nationality) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.nationality = nationality;
}
public Reng() {
super();
}
//人的方法
public void eat(){
System.out.println(“吃饭!”);
}
public void sleep(){
System.out.println(“睡觉!”);
}
public abstract void wook();
public abstract void shoInfo();
}
- package com.kgc.wook;
//学生类
public class Student extends Reng {
//学生特有的属性
private String school;
private int studentId;
//set get方法
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
//学生的属性的构造方法
public Student(String name, String sex, int age, String nationality,
String school, int studentId) {
super(name, sex, age, nationality);
this.school = school;
this.studentId = studentId;
}
public void wook() {
System.out.println(“我是学生,我要好好学习!”);
}
public void shoInfo() {
System.out.println(“我叫”+super.getName()+",是一名"+super.getSex()+
“学生”+";我在"+this.school+“读书”+",是一名"+super.getNationality()+“人”+
“;我的学号是;”+this.studentId);
}
}
- package com.kgc.wook;
//学生干部类
public class StudentCardres extends Student {
//学生干部特有的属性
private String post;
//构造方法
public StudentCardres(String name, String sex, int age, String nationality,
String school, int studentId,String post) {
super(name, sex, age, nationality, school, studentId);
this.post=post;
}
//学生干部的工作
public void wook() {
System.out.println(“我是学生会的,牛逼,能开会哦!”);
}
public void wooks(){
System.out.println(“我也是学生,也要好好学习的哦!”);
}
public void shoInfo() {
System.out.println(“我叫”+super.getName()+",是一名"+super.getSex()+
“学生”+";我在"+super.getSchool()+“读书”+",是一名"+super.getNationality()+“人”+
“;我的学号是;”+super.getStudentId()+“是学生干部,职位是”+this.post);
}
}
- package com.kgc.wook;
public class Test {
public static void main(String[] args) {
Reng v1=new Student(“小二哥”, “男”, 19, “中国”, “北京科技学院”, 3450268);
Reng v2=new StudentCardres(“小马哥”, “男”, 19, “中国”, “北京科技学院”, 3455088, “会长”);
Reng v3=new Worker(“农民大哥”, “男”, 29, “中国”,5, “中国科技技术研发有限公司”);
v1.wook();
v1.shoInfo();
v2.wook();
v2.shoInfo();
v3.wook();
v3.shoInfo();
}
}
- package com.kgc.wook;
public class Worker extends Reng {
//工人特有的属性
private int workAge;//工龄
private String unti;//单位
//构造方法
public Worker(String name, String sex, int age, String nationality,
int workAge, String unti) {
super(name, sex, age, nationality);
this.workAge = workAge;
this.unti = unti;
}
//工人的工作方法
public void wook() {
System.out.println(“我是农民工,我要努力挣钱!”);
}
public void shoInfo() {
System.out.println(“我叫”+super.getName()+",是一名"+super.getSex()+
“工人”+",是一名"+super.getNationality()+“人”+
“;我的工作单位是;”+this.unti);
}
}