Java第六次作業(yè)

167 - 學(xué)生列表

Time Limit: 1000? Memory Limit: 65535

Submit: 93? Solved: 53

Description

編寫學(xué)生類竭钝,包含學(xué)號(hào)no洼哎、姓名name、成績score躬充,提供必要的構(gòu)造函數(shù)口糕、toString函數(shù)和equals/hashcode函數(shù)譬胎,其中,toString函數(shù)的格式為“no:xxx name:xxx score:xxx”,no參與equals和hashcode的計(jì)算

在main函數(shù)中構(gòu)造一個(gè)學(xué)生列表對(duì)象(List)爬立,用于存放多個(gè)學(xué)生對(duì)象

從命令行輸入多個(gè)學(xué)生對(duì)象钾唬,存入列表中

從命令行中讀入在列表對(duì)象上的操作,具體操作包含:

add 添加一個(gè)學(xué)生(包含學(xué)號(hào)和學(xué)生姓名)

delete 刪除一個(gè)學(xué)生(包含學(xué)號(hào))

set 修改一個(gè)學(xué)生信息(只修改某學(xué)號(hào)學(xué)生的成績)

完成操作后按列表順序輸出集合中的學(xué)生

Input

學(xué)生個(gè)數(shù)

學(xué)生對(duì)象數(shù)據(jù)

操作數(shù)

操作內(nèi)容

Output

列表順序輸出集合中的學(xué)生

Sample Input

4

1 wong 90

2 liu 80

3 chen 70

4 fang 60

3

add 5 duan 80

delete 3

set 4 70

Sample Output

no:1 name:wong score:90

no:2 name:liu score:80

no:4 name:fang score:70

no:5 name:duan score:80

_______________________________

import java.util.*;

public class Main {

? ? public static void main(String[] args){

? ? ? ? List<Student> list=new LinkedList<Student>();

? ? ? ? Scanner scan = new Scanner(System.in);

? ? ? ? int num=scan.nextInt();

? ? ? ? for(int i=0;i<num;i++) {

? ? ? ? int no=scan.nextInt();

? ? ? ? String name=scan.next();

? ? ? ? int score=scan.nextInt();

? ? ? ? list.add(new Student(no,name,score));

? ? ? ? }

? ? ? ? int opnum=scan.nextInt();

? ? ? ? for(int i=0;i<opnum;i++) {

? ? ? ? String op=scan.next();


? ? ? ? if(op.equals("add")) {

? ? ? ? int no=scan.nextInt();

? ? ? ? ? ? String name=scan.next();

? ? ? ? ? ? int score=scan.nextInt();

? ? ? ? list.add(new Student(no,name,score));

? ? ? ? }

? ? ? ? else if(op.equals("delete")) {

? ? ? ? int n=scan.nextInt();

? ? ? ? for(int j=0;j<num;j++){

? ? ? ? ? ? ? ? ? ? if(list.get(j).no==n){

? ? ? ? ? ? ? ? ? ? ? ? list.remove(j);

? ? ? ? ? ? ? ? ? }

? ? ? ? }

? ? ? ? }

? ? ? ? else if(op.equals("set")) {

? ? ? ? int pos=scan.nextInt();

? ? ? ? int s=scan.nextInt();

? ? ? ? for(int j=0;j<num;j++){

? ? ? ? ? ? ? ? ? ? if(list.get(j).no==pos){

? ? ? ? ? ? ? ? ? ? ? ? list.set(j,new Student(list.get(j).no,list.get(j).name,s));

? ? ? ? ? ? ? ? ? ? }

? ? ? ? }

? ? ? ? }

? ? ? ? }

? ? ? ? for(Student s:list)

? ? ? ? System.out.println(s.toString());

? ? }

}

class Student {

? ? int no;

? ? String name;

? ? int score;


? ? public Student(int _no, String _name, int _score) {

? ? ? ? no = _no;

? ? ? ? name = _name;

? ? ? ? score = _score;

? ? }

? ? public int getNo() {return no;}

? ? public String getName() {return name;}

? ? public int getScore() {return score;}

public String toString() {

? ? ? return "no:"+no+" name:"+name+" score:"+score;

}

? ? public boolean equals(Object o) {

? ? if (o == null) return false;

? ? else {

boolean result = false;

if (o instanceof Student) {

Student rec = (Student) o;

? ? ? if (this.no == rec.no&&this.name.equals(((Student) o).name)) {

? ? ? ? ? ? result = true;

? ? ? }

? ? ? ? ? ? ? }

? ? ? ? ? ? return result;

? ? }

? ? }

? ? public int hashcode() {

? ? int result = 17; // 任意素?cái)?shù)

? ? result = 31 * result + no;

? ? result = 31* result + name.hashCode();

? ? return result;

? ? }

}

###########################################

168 - 學(xué)生Map

Time Limit: 1000? Memory Limit: 65535

Submit: 109? Solved: 76

Description

修改《學(xué)生列表》題目侠驯,使用學(xué)生Map來存放學(xué)生的集合抡秆,其中key為學(xué)號(hào),value為學(xué)生對(duì)象

輸出時(shí)按照學(xué)生的學(xué)號(hào)順序輸出

Input

學(xué)生個(gè)數(shù)

學(xué)生對(duì)象數(shù)據(jù)

操作數(shù)

操作內(nèi)容

Output

按照學(xué)號(hào)順序輸出集合中的學(xué)生

Sample Input

4

1 wong 90

2 liu 80

3 chen 70

4 fang 60

3

add 5 duan 80

delete 3

set 4 70

Sample Output

no:1 name:wong score:90

no:2 name:liu score:80

no:4 name:fang score:70

no:5 name:duan score:80

___________________________

import java.util.*;

public class Main {

? ? public static void main(String[] args){

? ? Map<Integer,Student> map = new HashMap<Integer,Student>();

? ? ? ? Scanner scan = new Scanner(System.in);

? ? ? ? int num=scan.nextInt();

? ? ? ? for(int i=0;i<num;i++) {

? ? ? ? int no=scan.nextInt();

? ? ? ? String name=scan.next();

? ? ? ? int score=scan.nextInt();

? ? ? ? map.put(no,new Student(no,name,score));

? ? ? ? }

? ? ? ? int opnum=scan.nextInt();

? ? ? ? for(int i=0;i<opnum;i++) {

? ? ? ? String op=scan.next();


? ? ? ? if(op.equals("add")) {

? ? ? ? int no=scan.nextInt();

? ? ? ? ? ? String name=scan.next();

? ? ? ? ? ? int score=scan.nextInt();

? ? ? ? ? ? map.put(no,new Student(no,name,score));

? ? ? ? }

? ? ? ? else if(op.equals("delete")) {

? ? ? ? int n=scan.nextInt();

? ? ? ? ? ? ? ? map.remove(n);

? ? ? ? }

? ? ? ? else if(op.equals("set")) {

? ? ? ? int pos=scan.nextInt();

? ? ? ? int s=scan.nextInt();

? ? ? ? for(Student stu: map.values()){

? ? ? ? ? ? ? ? ? ? if(stu.getNo()==pos)

? ? ? ? ? ? ? ? ? ? ? ? stu.setScore(s);

? ? ? ? ? ? ? ? }

? ? ? ? }

? ? ? ? }

? ? ? ? for(Student s:map.values())

? ? ? ? System.out.println(s.toString());

? ? }

}

class Student {

? ? int no;

? ? String name;

? ? int score;


? ? public Student(int _no, String _name, int _score) {

? ? ? ? no = _no;

? ? ? ? name = _name;

? ? ? ? score = _score;

? ? }

? ? public int getNo() {return no;}

? ? public String getName() {return name;}

? ? public int getScore() {return score;}

? ? void setScore(int score) {

? ? this.score=score;

? ? }

public String toString() {

? ? ? return "no:"+no+" name:"+name+" score:"+score;

}

? ? public boolean equals(Object o) {

? ? if (o == null) return false;

? ? else {

boolean result = false;

if (o instanceof Student) {

Student rec = (Student) o;

? ? ? if (this.no == rec.no&&this.name.equals(((Student) o).name)) {

? ? ? ? ? ? result = true;

? ? ? }

? ? ? ? ? ? ? }

? ? ? ? ? ? return result;

? ? }

? ? }

? ? public int hashcode() {

? ? int result = 17; // 任意素?cái)?shù)

? ? result = 31 * result + no;

? ? result = 31* result + name.hashCode();

? ? return result;

? ? }

}

##########################################

184 - 4

Time Limit: 1000? Memory Limit: 65535

Submit: 140? Solved: 40

Description

在上題的基礎(chǔ)上構(gòu)建一個(gè)書單類BookList吟策,該類中用一個(gè)列表類對(duì)象存放書單儒士,提供添加圖書(addBook)、查找圖書(searchBook)的函數(shù)

main函數(shù)從鍵盤輸入多個(gè)Book添加到書單中檩坚,(添加時(shí)着撩,提供書的名稱、價(jià)格匾委、作者拖叙、版本號(hào)),而后從鍵盤讀入一本書赂乐,查找該列表對(duì)象中是否包含該書薯鳍,若包含,輸出”found: 該書在列表中的序號(hào)”挨措,若不包含挖滤,輸出“not found”崩溪,查找時(shí),提供書的名稱壶辜、作者悯舟、版本號(hào)。

Input

添加書的個(gè)數(shù)

添加的書

查找的書

Output

查找結(jié)果

Sample Input

2

ThinkingInJava

86

BruceEckel

4

CoreJava

95

CayS.Horstmann

10

CoreJava

CayS.Horstmann

10

Sample Output

found: 1

________________________________________

import java.util.*;

public class Main{

public static void main(String[] args) {d

Scanner s = new Scanner(System.in);

BookList bl = new BookList();

int n = s.nextInt();

for (int i=0; i<n;i++) {

bl.addBook(new Book(s.next(),

s.nextInt(),

s.next(),

s.nextInt()));

}

bl.searchBook(new Book(s.next(),

0,

s.next(),

s.nextInt()));

}

}

class Book{

String name;

int price;

String author;

int no;

Book(String name,int price,String author,int no){

this.name=name;

this.price=price;

this.author=author;

this.no=no;

}

}

class BookList{

List<Book> book=new ArrayList<Book>();

void addBook(Book b) {

book.add(b);

}

void searchBook(Book B) {

for(Book b:book) {

if(b.name.equals(B.name)&&b.author.equals(B.author)&&b.no==B.no) {

System.out.println("found: "+book.indexOf(b));

? ? ? ? ? ? ? ? return ;

}

}

System.out.println("not found");

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末砸民,一起剝皮案震驚了整個(gè)濱河市抵怎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌岭参,老刑警劉巖反惕,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異演侯,居然都是意外死亡姿染,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門秒际,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悬赏,“玉大人,你說我怎么就攤上這事娄徊∶銎模” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵寄锐,是天一觀的道長兵多。 經(jīng)常有香客問我,道長橄仆,這世上最難降的妖魔是什么剩膘? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮盆顾,結(jié)果婚禮上怠褐,老公的妹妹穿的比我還像新娘。我一直安慰自己您宪,他們只是感情好惫搏,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蚕涤,像睡著了一般筐赔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上揖铜,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天茴丰,我揣著相機(jī)與錄音,去河邊找鬼。 笑死贿肩,一個(gè)胖子當(dāng)著我的面吹牛峦椰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播汰规,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼汤功,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了溜哮?” 一聲冷哼從身側(cè)響起滔金,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎茂嗓,沒想到半個(gè)月后餐茵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡述吸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年忿族,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蝌矛。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡道批,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出入撒,到底是詐尸還是另有隱情隆豹,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布衅金,位于F島的核電站,受9級(jí)特大地震影響簿煌,放射性物質(zhì)發(fā)生泄漏氮唯。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一姨伟、第九天 我趴在偏房一處隱蔽的房頂上張望惩琉。 院中可真熱鬧,春花似錦夺荒、人聲如沸瞒渠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽伍玖。三九已至,卻和暖如春剿吻,著一層夾襖步出監(jiān)牢的瞬間窍箍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留椰棘,地道東北人纺棺。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像邪狞,于是被迫代替她去往敵國和親祷蝌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 140 - 家電類 Time Limit: 1000 Memory Limit: 65535 Submit: 1...
    z坎坷閱讀 640評(píng)論 0 0
  • 131 員工類 import java.util.*; public class Main { public st...
    z坎坷閱讀 283評(píng)論 0 0
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法帆卓,類相關(guān)的語法巨朦,內(nèi)部類的語法,繼承相關(guān)的語法鳞疲,異常的語法罪郊,線程的語...
    子非魚_t_閱讀 31,661評(píng)論 18 399
  • 今年除夕前一天,小貓梨名和雪友因不團(tuán)結(jié)友愛發(fā)生撕斗尚洽,而后雪友落敗悔橄,不知所蹤。我讓店面的柬埔寨工人——少年阿正...
    溫婉媚閱讀 504評(píng)論 0 0
  • 其實(shí)腺毫,是已經(jīng)失去聯(lián)系的一個(gè)朋友癣疟,大概也是我朋友里最年長的一個(gè),不過他一直說自己和我們沒有代溝潮酒,暫且相信他好了睛挚。 認(rèn)...
    這個(gè)景閱讀 319評(píng)論 0 2