java代碼詳情

書寫的練習代碼

  1. 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();
    }
    }
  2. 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();
}

}

  1. 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();
}

}

  1. 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() ;
    }

}

  1. 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();
    }

  2. 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();
}

	}
}

}

  1. 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();
}

  1. 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();

}

}

  1. 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+“秒!”);
}
}

}

  1. 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();
}

  1. 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);
}

}

  1. 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);
}

}

  1. 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();

}

}

  1. 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);

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章