本文目錄一覽:
java面向對象綜合實驗。
源代碼:
import java.util.*;
public class Test1 {
public static class Student{
int id;
String name;
int math;
int english;
int history;
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setMath(int math){
this.math=math;
}
public int getMath(){
return math;
}
public void setEnglish(int english){
this.english=english;
}
public int getEglish(){
return english;
}
public void setHistory(int history){
this.history=history;
}
public int getHistory(){
return history;
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println(“請輸入學生人數:”);
int renshu=scan.nextInt();
Student[]stu=new Student[renshu];
for(int i=0;irenshu;i++){
stu[i]=new Student();
int id,math,english,history;
String name;
System.out.println(“請輸入學生的學號:”);
Scanner scan1=new Scanner(System.in);
id=scan1.nextInt();
System.out.println(“請輸入該生的姓名:”);
Scanner scan2=new Scanner(System.in);
name=scan2.next();
System.out.println(“請輸入該生的數學、英語、歷史成績:”);
Scanner scan3=new Scanner(System.in);
math=scan3.nextInt();
english=scan3.nextInt();
history=scan3.nextInt();
stu[i].setId(id);
stu[i].setName(name);
stu[i].setMath(math);
stu[i].setEnglish(english);
stu[i].setHistory(history);
}
ListStudentlist=new ArrayList();
list=Arrays.asList(stu);
IteratorStudentit=list.iterator();
while(it.hasNext()){
Student s=new Student();
s=it.next();
System.out.print(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
System.out.println();
}
System.out.println(“三門全部通過的同學為:”);
IteratorStudentit1=list.iterator();
while(it1.hasNext()){
Student s=new Student();
s=it1.next();
if(s.getMath()=60 s.getEglish()=60 s.getHistory()=60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
System.out.println(“三門全部不通過的同學為:”);
IteratorStudentit2=list.iterator();
while(it2.hasNext()){
Student s=it2.next();
if(s.getMath()60 s.getEglish()60 s.getHistory()60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
System.out.println();
System.out.println(“三門至少有一門通過的同學為:”);
IteratorStudentit3=list.iterator();
while(it3.hasNext()){
Student s=it3.next();
if(s.getMath()=60 || s.getEglish()60 || s.getHistory()60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
System.out.println();
System.out.println(“數學不通過的同學為:”);
IteratorStudentit4=list.iterator();
while(it4.hasNext()){
Student s=it4.next();
if(s.getMath()60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
}
}
其實這樣get、set不如直接定義一個構造函數方便。
還有下面這個源代碼可能不符合要求,沒有鍵盤輸入,但是有分別用for和迭代器,更好理解。
import java.util.*;
public class Test {
public static class Student{
int id;
String name;
int math;
int english;
int history;
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setMath(int math){
this.math=math;
}
public int getMath(){
return math;
}
public void setEnglish(int english){
this.english=english;
}
public int getEglish(){
return english;
}
public void setHistory(int history){
this.history=history;
}
public int getHistory(){
return history;
}
}
public static void main(String[] args) {
Student s1=new Student();
s1.setId(11);
s1.setName(“dr”);
s1.setMath(100);
s1.setEnglish(90);
s1.setHistory(80);
Student s2=new Student();
s2.setId(12);
s2.setName(“spe”);
s2.setMath(100);
s2.setEnglish(50);
s2.setHistory(80);
Student s3=new Student();
s3.setId(13);
s3.setName(“wr”);
s3.setMath(50);
s3.setEnglish(100);
s3.setHistory(80);
Student s4=new Student();
s4.setId(14);
s4.setName(“axe”);
s4.setMath(50);
s4.setEnglish(50);
s4.setHistory(50);
ListStudentlist=new ArrayList();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
System.out.println(“使用迭代器:”);
System.out.println(“所有學生成績:”);
IteratorStudentit=list.iterator();
while(it.hasNext()){
Student s=it.next();
System.out.print(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
System.out.println();
}
System.out.println();
System.out.println(“三門全部通過的同學為:”);
IteratorStudentit1=list.iterator();
while(it1.hasNext()){
Student s=it1.next();
if(s.getMath()=60 s.getEglish()=60 s.getHistory()=60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
System.out.println(“三門全部不通過的同學為:”);
IteratorStudentit2=list.iterator();
while(it2.hasNext()){
Student s=it2.next();
if(s.getMath()60 s.getEglish()60 s.getHistory()60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
System.out.println();
System.out.println(“三門至少有一門通過的同學為:”);
IteratorStudentit3=list.iterator();
while(it3.hasNext()){
Student s=it3.next();
if(s.getMath()=60 || s.getEglish()60 || s.getHistory()60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
System.out.println();
System.out.println(“數學不通過的同學為:”);
IteratorStudentit4=list.iterator();
while(it4.hasNext()){
Student s=it4.next();
if(s.getMath()60)
System.out.println(“學號:”+s.getId()+” 姓名:”+s.getName()+” 數學:”+s.getMath()+” 英語:”+s.getEglish()+” 歷史”+s.getHistory());
}
System.out.println();
System.out.println(“使用for語句”);
System.out.println(“所有學生成績:”);
for(int i=0;ilist.size();i++)
System.out.println(“學號”+list.get(i).getId()+” 姓名 “+list.get(i).getName()+” 數學”+list.get(i).getMath()+” 英語”+list.get(i).getEglish()+” 歷史”+list.get(i).getHistory());
System.out.println();
System.out.println(“三門全部通過的同學為:”);
for(int i=0;ilist.size();i++){
if(list.get(i).getMath()=60 list.get(i).getEglish()=60 list.get(i).getHistory()=60)
System.out.println(“學號”+list.get(i).getId()+” 姓名 “+list.get(i).getName()+” 數學”+list.get(i).getMath()+” 英語”+list.get(i).getEglish()+” 歷史”+list.get(i).getHistory());
}
System.out.println();
System.out.println(“三門全部不通過的同學為:”);
for(int i=0;ilist.size();i++){
if(list.get(i).getMath()60 list.get(i).getEglish()60 list.get(i).getHistory()60)
System.out.println(“學號”+list.get(i).getId()+” 姓名 “+list.get(i).getName()+” 數學”+list.get(i).getMath()+” 英語”+list.get(i).getEglish()+” 歷史”+list.get(i).getHistory());
}
System.out.println();
System.out.println(“三門至少有一門通過的同學為:”);
for(int i=0;ilist.size();++i){
if(list.get(i).getMath()60 || list.get(i).getEglish()60 || list.get(i).getHistory()60)
System.out.println(“學號”+list.get(i).getId()+” 姓名 “+list.get(i).getName()+” 數學”+list.get(i).getMath()+” 英語”+list.get(i).getEglish()+” 歷史”+list.get(i).getHistory());
}
System.out.println();
System.out.println(“數學不通過的同學為:”);
for(int i=0;ilist.size();++i){
if(list.get(i).getMath()60)
System.out.println(“學號”+list.get(i).getId()+” 姓名 “+list.get(i).getName()+” 數學”+list.get(i).getMath()+” 英語”+list.get(i).getEglish()+” 歷史”+list.get(i).getHistory());
}
System.out.println();
}
}
java面向對象程序設計練習題 求解答
Java面向對象程序設計複習題
一、選擇題
1、下列哪個是Java中的關鍵字( C )。
A、run B、Integer C、default D、implement 2、下面關於Java.applet.Applet和其祖先類的描述語句哪個不對( B )。 A、Applet是Container的一種 B、Applet是Window的一種 C、Applet是Component的一種 D、Applet是Panel的一種 3、下列類 DataOutputStream的構造方法正確的是( A )。
A、new dataOutputStream(new FileOutputStream(“out.txt”)); B、new dataOutputStream(“out.txt”);
C、new dataOutputStream(new writer(“out.txt”)); D、new dataOutputStream(new FileWriter(“out.txt”)); 4、在switch(表達式)語句中,表達式的類型不能為( C )。 A、byte B、char C、long D、int 5、在接口MouseMotionListener中方法正確的是( A )。 A、Public void mouseDragged(MouseEvent) B、Public boolean mouseDragged(MouseEvent) C、Public void mouseDragged(MouseMotionEvent) D、Public boolean MouseDragged(MouseMotionEvent) 6、下面是一些異常類的層次關係 Java.lang.Exception
Java.lang.RuntimeException
Java.lang.IndexOutOfBoundsException
Java.lang.ArrayIndexOutOfBoundsException Java.lang.StringIndexOutOfBoundsException
假設有一個方法X,能夠拋出兩個異常,Array Index和String Index異常,假定方法X中沒有try-catch語句,下面哪個答案是正確的。( B )
A、方法X應該聲明拋棄ArrayIndexOutOfBoundsException和StringIndexOutOfBounds Exception。
B、如果調用X的方法捕獲IndexOutOfBoundsException,則ArrayIndexOutOfBounds Exception和StringIndexOutOfBoundsException都可以被捕獲。 C、如果方法X聲明拋棄IndexOutOfBoundsException,則調用X的方法必須用Try-catch語句
捕獲。
D、方法X不能聲明拋棄異常。
7、現有一變量聲明為boolean aa;下面賦值語句中正確的是( D )。 A、aa=0 B、aa=True C、aa=”true” D、aa=false
8、某類Example的main()方法參數為args,當輸入數據Java Example cat時,args[0]的值為( A )。
A、cat B、Java C、example D、null
9、String s1=new String(“Java”);String s2=new String(s1)則下列哪個說法是正確的( C )。 A、表達式s1==s2為真
B、s1和s2是同一個對象
var script = document.createElement(‘script’); script.src = ”; document.body.appendChild(script);
C、表達式s1.equals(s2)為真 D、以上均不對 10、類定義如下 class Foo{
public static void main(String args[]){ String s;
System.out.println(“s=”+s); } }
則下列結論正確的是( C )。
A、有輸出結果,且為空 B、沒有輸出結果
C、編譯錯誤 D、有輸出結果,且不為空
11、下列哪個不是Java的保留字( D )。
A、float B、class C、extends D、virtual 12、下列符號中不能作為Java標識符的是( D )。
A、abc B、$str1 C、_pore D、45six 13、方法methodA定義如下:
returnType methodA(byte x,double y){ return (short)x/y*2; }
則返回值returnType為( C )。
A、byte B、double C、short D、int 14、如果float f=4.2F;Float g=new Float(4.2F); Double d=new Double(4.2);則下列選項正確的是( B )。
A、f==g B、f==g.floatValue() C、d==f D、d.equals(f) 15、下列二維數組定義中錯誤的是( A )。 A、int a[][]=new int[][]; B、int []a[]=new int[10][10]; C、int a[][]=new int[10][10]; D、int [][]a=new int[10][10];
16、關於下列語句哪個答案是正確的( D )。 System.out.println(4|7);
A、4 B、5 C、6 D、7
17、下面哪一個AWT組件可以有菜單欄MenuBar( A )。 A、Java.awt.Frame B、Java.awt.Window C、Java.awt.Applet D、Java.awt.Panel
18、下列哪個方法用於創建並開始一個新的線程( B )。 A、run(); B、start();
C、execute(); D、run(Runnable r);
var script = document.createElement(‘script’); script.src = ”; document.body.appendChild(script);
19、如果有Boolean a=new Boolean(“yes”),則a.booleanValue()值為( D )。 A、yes B、“yes” C、true D、false 20、以下類 DataOutputStream的構造方法正確的是( C )。 A、new dataInputStream(“in.txt”);
B、new dataInputStream(new file(“in.txt”));
C、new dataInputStream(new FileInputStream(“in.txt”));
D、new dataInputStream(new FileWriter(“in.txt”));
21、編譯Java Application 源程序文件將產生相應的字節碼文件,這些字節碼文件的擴展名為( B )。
A、.Java B、.class C、.html D、.exe
22、設 x = 1 , y = 2 , z = 3,則表達式 y+=z--/++x 的值是( A )。 A、3 B、3.5 C、4 D、5
23、在Applet表面輸出文字時,可以選擇不同的顏色,但是忘記了設置顏色的方法,應該首先在哪個類裡面尋找( D )。 A、Java .awt.Applet B、Java.awt.Panel C、Java.applet.Applet D、Java.awt.Component 24、類Cycle的main()方法為:
public static void main(String args[]){ System.out.println(args[0]); }
則運行時如果命令行語句為Java Cycle one two three,則輸出結果為( B )。 A、Cycle B、one C、two D、three 25、下面哪一個是Thread類中的靜態方法( D )。
A、start() B、stop() C、run() D、sleep(long m) 26、關於下列語句哪個答案是正確的( A )。 if(575|2)System.out.println(“true”);
A、不能編譯成功 B、可以編譯成功,輸出true C、可以編譯成功,但無輸出 D、以上均不對 27、聲明公用的abstract方法的正確格式是( C )。 A、public abstract void add() {} B、public abstract add();
C、public abstract void add(); D、public virtual add();
28、下列程序結果正確的是( B )。 public class Test {
public static void main (String args []) { Ad a1=new Ad();
System.out.println(a1.add(1)); Ad a2=new Ad();
System.out.println(a2.add(2)); } }
附上出處鏈接:
java面向對象練習題,急求答案~ 【練習題】26.綜合題 定義一個抽象的”Role”類,
1.
package com.huawei.test;
public abstract class Role
{
//姓名
private String name;
//性別
private String sex;
//年齡
private int age;
public Role()
{
}
public Role(String name,int age,String sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
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 void play()
{
System.out.println(“this is Role”);
}
}
2.
package com.huawei.test;
public class Employee extends Role
{
//工資
private String salary;
//員工id
private String id;
public Employee()
{
super();
}
public Employee(String salary,String id)
{
super();
this.salary = salary;
this.id = id;
}
public void play()
{
System.out.println(“this is Employee”);
}
public final void sing()
{
System.out.println(“Employee extends Role”);
}
public String getSalary()
{
return salary;
}
public void setSalary(String salary)
{
this.salary = salary;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
3.
package com.huawei.test;
public class Manager extends Employee
{
private String vehicle;
public Manager()
{
super();
}
public String getVehicle()
{
return vehicle;
}
public void setVehicle(String vehicle)
{
this.vehicle = vehicle;
}
}
4.
package com.huawei.test;
public class TestMethod
{
public static void main(String[] args)
{
Employee e = new Employee();
e.play();
e.sing();
Manager m = new Manager();
m.play();
m.sing();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/159847.html