一、判斷題
1-1 抽象類(lèi)是不能實(shí)例化的。?T?
1-2?JAVA抽象類(lèi)中一定含有抽象方法。?F?
答題時(shí)沒(méi)有看到一定qaq,抽象類(lèi)不一定包含抽象方法,但包含抽象方法的類(lèi)一定是抽象類(lèi)。
二、單選題
2-2 有如下程序代碼, 程序運(yùn)行的結(jié)果是( )。
String s1 = "sdut";
String s2 = "I love " + s1;
String s3 = "I love " + s1;
System.out.print(s2 == s3);
Sytem.out.println(" "+s2.equals(s3));
D.false true
第一個(gè)竟然是false?。?!
使用“==”比較兩個(gè)字符串,是比較兩個(gè)對(duì)象的地址是否一致,本質(zhì)是判斷兩個(gè)變量是否指向同一個(gè)對(duì)象;String類(lèi)的equals方法是比較兩個(gè)字符串的內(nèi)容是否一致,返回值也是一個(gè)bool類(lèi)型。
2-2?關(guān)于字符串的方法,如下代碼執(zhí)行結(jié)果是( )。
String str = " abcd123";
str.toUpperCase();
str.trim();
System.out.println(str);
A.“? ? abcd123
”
兩個(gè)方法未對(duì)str進(jìn)行賦值;
toUpperCase對(duì)StringObject的所有小寫(xiě)字符全部轉(zhuǎn)換為大寫(xiě)字符
trim方法刪除stringObject的頭尾空白符(僅有頭尾)。
三、填空題
3-1 在映射(Map)集合中查找指定關(guān)鍵字的元素
本題目要求輸入關(guān)鍵字,在映射(Map)集合中進(jìn)行查找。
import java.util.*;
/* 這里是 People 類(lèi)代碼,無(wú)需關(guān)心
*/
public class Main
{
public static void main(String args[])
{
Map<String,People> peoples = new HashMap<String,People>();
peoples.put("rose", new People("rose",18));
peoples.put("hunifu", new People("hunifu",19));
peoples.put("hunifu", new People("britsh",20));
if(①) //@@正確判斷 Map 元素個(gè)數(shù)
{
Scanner sc=new Scanner(System.in);
//通過(guò)new Scanner(System in)創(chuàng)建一個(gè)Scanner,控制臺(tái)會(huì)一直等待輸入
String key=sc.next();
if(②)//@@判斷關(guān)鍵字是否存在 Map 內(nèi)
System.out.println(③);//@@取出關(guān)鍵字指定元素
else
System.out.println("映射中不存在 key ="+key+" 的元素");
}
}
}
①peoples.size() > 0?
②peoples.containsKey(key)
③peoples.get(key)
https://blog.csdn.net/m0_66298628/article/details/125237634
Java中super()的使用_程序員資料站的博客-CSDN博客_super()
3-2?類(lèi)和對(duì)象的創(chuàng)建和使用
定義一個(gè)Person類(lèi),該類(lèi)包括age,name兩個(gè)數(shù)據(jù)成員和eat(),work()兩個(gè)成員方法,并實(shí)現(xiàn)對(duì)兩個(gè)數(shù)據(jù)成員的Getter方法。然后通過(guò)測(cè)試程序,實(shí)現(xiàn)相應(yīng)對(duì)象的操作。程序輸出結(jié)果如下:
請(qǐng)按提示完善相應(yīng)的程序。
class Person
{
public int age; //聲明公共字段age
①; //聲明私有字段name
public int getAge()//實(shí)現(xiàn)age成員的Getter方法
{
return age;
}
public String getName() //實(shí)現(xiàn)name成員的Getter方法
{
②;
}
public void eat() //定義無(wú)參數(shù)的eat方法
{
System.out.println(③);
}
public void work(String s) //定義帶參數(shù)的work方法
{
System.out.println("努力創(chuàng)造"+s);
}
public Person(int age, String name) //定義一個(gè)帶參數(shù)構(gòu)造函數(shù)
{
this.age = age; this.name = name;
}
}
public class Main
{
public static void main(String[] args)
{
Person p =④(18, "張三");//調(diào)用類(lèi)的有參構(gòu)造函數(shù)
System.out.println("姓名:"+p.getName()+"\n年齡:" + p.getAge());
⑤; //調(diào)用對(duì)象的eat方法
p.work("美好生活");
}
}
①private String name
②return name
③"會(huì)生活"
④new Person
⑤p.eat()
3-3 計(jì)算每個(gè)雇員每周工作的小時(shí)數(shù)并排序
假定所有雇員每周工作的小時(shí)數(shù)存儲(chǔ)在一個(gè)二維數(shù)組中。1行包含7列,記錄了一個(gè)雇員7天的工作小時(shí)數(shù)。編寫(xiě)一個(gè)程序,按照總工時(shí)降序的方式顯示雇員和他們的總工時(shí)。
public class Main
{
/** Main method */
public static void main(String[] args)
{
// Declare, create, and initialized array
double[][] workHours =
{
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}
};
// Create an array to store total weekly hours
int[] weeklyHours = new int[workHours.length];
for (int i = 0; i < workHours.length; i++)
for (int j = 0; j < workHours[i].length; j++)
weeklyHours[i] += ①;
int[] indexList = new int[weeklyHours.length];
// Sort weeklyHours
sortAndKeepIndex(weeklyHours, indexList);
// Display result
for (int i = weeklyHours.length - 1; i >= 0; i--)
System.out.println("Employee " + indexList[i] + ": " +②);
}
/** The method for sorting the numbers */
static void sortAndKeepIndex(int[] ③,④indexList)
{
int currentMax; int currentMaxIndex;
// Initialize indexList
for (int i = 0; i < indexList.length; i++)
indexList[i] = i;
for (int i = list.length - 1; i >= 1; i--)
{
// Find the maximum in the list[0..i]
currentMax = list[i];
currentMaxIndex = ⑤;
for (int j = i - 1; j >= 0; j--)
{
if (currentMax < list[j])
{
currentMax = ⑥;
currentMaxIndex = j;
}
}
// Swap list[i] with list[currentMaxIndex] if necessary;
if (currentMaxIndex != i)
{
list[currentMaxIndex] = list[i];
list[i] = currentMax;
// Swap the index in indexList too
int temp = ⑦;
⑧ = indexList[currentMaxIndex];
indexList[currentMaxIndex] = temp;
}
}
}
}
①workHours[i][j]
②weeklyHours[i]
③list
④int[]
⑤indexList[i]
⑥list[j]
⑦indexList[i]
⑧indexList[i]
3-4?矩形類(lèi)
構(gòu)造一個(gè)Rectangle類(lèi)表示矩形 , 該類(lèi)實(shí)現(xiàn)了Comparable接口。
- 該類(lèi)有兩個(gè)私有的double型成員變量width和height,表示矩形的寬度和高度;
- 該類(lèi)有一個(gè)帶一個(gè)無(wú)參的構(gòu)造方法,把寬帶和高度都初始化為0;
- 該類(lèi)有一個(gè)帶兩個(gè)double型參數(shù)的構(gòu)造方法,用參數(shù)的值初始化矩形的寬度和高度。
- 為width和height添加getter()和setter()方法。注意,寬度和高度必須大于等于0,如果setWidth(double width)方法的參數(shù)小于0時(shí),拋出一個(gè)IllegalArgumentException異常對(duì)象,異常對(duì)象的成員變量message為"矩形的寬度必須大于等于0";setHeight(double height)方法的參數(shù)小于0時(shí),拋出一個(gè)IllegalArgumentException異常對(duì)象,異常對(duì)象的成員變量message為"矩形的高度必須大于等于0"。
- getArea()方法,返回矩形的面積;
- getgetPerimeter()方法,返回矩形的周長(zhǎng);
- 該類(lèi)有一個(gè)公共的方法int compareTo(Rectangle rectangle),比較當(dāng)前對(duì)象和參數(shù)。如果當(dāng)前對(duì)象的面積大于參數(shù)的面積,返回1;當(dāng)前對(duì)象的面積小于參數(shù)的面積,返回-1;否則返回0。
- 該類(lèi)有一個(gè)公共方法toString(),根據(jù)矩形的寬度和高度生成并返回一個(gè)字符串(具體要求看輸出樣例)。
構(gòu)造一個(gè)Main ,執(zhí)行一個(gè)for循環(huán),共循環(huán)6次。每次循環(huán)從鍵盤(pán)讀入數(shù)據(jù)創(chuàng)建兩個(gè)矩形對(duì)象,比較并輸出其中較大的對(duì)象的面積;如果捕捉到異常,則輸出異常信息。
①
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
for(int i=0; i<6; i++)
{
②
{
Rectangle rectangle1 = new Rectangle(input.nextDouble(), input.nextDouble());
Rectangle rectangle2 = new Rectangle(input.nextDouble(), input.nextDouble());
③
if (rectangle1.④(rectangle2)>= 0)
⑤
else
⑥
System.out.println("The max area of " + rectangle1 + " and " + rectangle2 + " is " + maxRectangle.getArea());
}
⑦(IllegalArgumentException e1)
{
System.out.println(e1.getMessage());
input.nextLine();
}
catch (Exception e2)
{
System.out.println(e2.getMessage());
input.nextLine();
}
}
}
}
interface Comparable
{
public double getWidth();
public void setWidth(double width);
public double getHeight();
public void setHeight(double height);
public double getArea();
public double getPerimeter();
public int compareTo(Rectangle rectangle);
public String toString();
}
class Rectangle ⑧
{
private double width;
private double height;
public ⑨()
{
width=0;
height=0;
}
public Rectangle(double width, double height)
{
⑩(width);
⑾(height);
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
if(⑿)
this.width = width;
else
⒀
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
if(⒁)
this.height = height;
else
⒂
}
public double getArea()
{
⒃
}
public double getPerimeter()
{
⒄
}
public int compareTo(Rectangle rectangle)
{
if (⒅)
return 1;
else
if (十九)
return -1;
else
return 0;
}
public String toString()
{
return 二十
}
}
接口Comparable為我另加的,因?yàn)轭}目中提到Rectangle類(lèi)實(shí)現(xiàn)了Comparable接口,但Java中沒(méi)有提供Comparable接口,需要自己定義;①Comparable中的各種方法不寫(xiě)也可以;②如果在Comparable前加上public,不論加在interface前面還是后面,如果寫(xiě)所有方法的聲明都會(huì)顯示錯(cuò)誤java: 需要<標(biāo)識(shí)符>;如果不在Comparable接口中寫(xiě)方法的聲明,顯示錯(cuò)誤java: 接口 Comparable 是公共的, 應(yīng)在名為 Comparable.java 的文件中聲明
Java 接口與實(shí)現(xiàn)(詳細(xì)版) (baidu.com)
類(lèi)接口_qppan_wx的博客-CSDN博客_類(lèi)接口
1、import java.util.*;
2、try
3、Rectangle maxRectangle;
4、compareTo
5、maxRectangle=rectangle1;
6、maxRectangle=rectangle2;
7、catch?
8、implements Comparable<Rectangle>
9、Rectangle
10、setWidth
11、setHeight
12、width>=0
13、throw new IllegalArgumentException("矩形的寬度必須大于等于0");
14、height>=0
15、throw new IllegalArgumentException("矩形的高度必須大于等于0");
16、return width*height;
17、return 2*(width+height);
18、getArea()>rectangle.getArea()
19、getArea()<rectangle.getArea()
20、"Rectangle(width:" + width + ",height:" +height + ")";
IllegalArgumentException(非法參數(shù)異常)和其子類(lèi)異常NumberFormatException,以及異常類(lèi)之間的繼承關(guān)系和產(chǎn)生原因_是小陳呀~的博客-CSDN博客_illegalargument
上面這一個(gè)沒(méi)有看明白,下面第一個(gè)是對(duì)上面那個(gè)暫時(shí)沒(méi)看懂的輔助
radix在Character.MIN_RADIX與Character.MAX_RADIX之間_ROBIN-KING的博客-CSDN博客_character.max_radix
Java中import java.util.Scanner;語(yǔ)句的作用_不愛(ài)吃番茄a(bǔ)a的博客-CSDN博客_import java.util.scanner;
try-catch的使用以及細(xì)節(jié)_呀吖呀吖呀的博客-CSDN博客_try-catch
5-5?自定義異常類(lèi):成績(jī)異常(ScoreException)
自定義一個(gè)異常類(lèi)ScoreException,繼承自Exception類(lèi)。有一個(gè)私有的成員變量message(異常提示信息,String類(lèi)型);一個(gè)公有的無(wú)參數(shù)的構(gòu)造方法,在方法中將message的值確定為“您輸入的成績(jī)異常,請(qǐng)核實(shí)!”;一個(gè)公有的方法show(),該方法的功能是輸出message的值。
定義一個(gè)學(xué)生類(lèi)Student,有一個(gè)私有成員變量score(成績(jī),double類(lèi)型);一個(gè)帶參數(shù)的公有方法setScore()用于設(shè)置學(xué)生的成績(jī),該方法聲明可能拋出異常ScoreException,當(dāng)設(shè)置的成績(jī)?yōu)樨?fù)數(shù)或超過(guò)100時(shí),會(huì)拋出一個(gè)異常對(duì)象;一個(gè)公有方法getScore()用于獲取學(xué)生的成績(jī)。
在測(cè)試類(lèi)Main中,創(chuàng)建一個(gè)Student類(lèi)的對(duì)象zhangsan,嘗試調(diào)用setScore()方法來(lái)設(shè)置他的成績(jī)(成績(jī)從鍵盤(pán)輸入,double類(lèi)型),然后調(diào)用getScore()方法獲取到該成績(jī)后再將其輸出。因用戶的輸入存在不確定性,以上操作有可能捕捉到異常ScoreException,一旦捕捉到該異常,則調(diào)用show()方法輸出異常提示。不管是否有異常,最終輸出“程序結(jié)束”。使用try...catch...finally語(yǔ)句實(shí)現(xiàn)上述功能。
import java.util.Scanner;
class ScoreException ①
{
private String message;
public ScoreException()
{
message = "您輸入的成績(jī)異常,請(qǐng)核實(shí)!";
}
public void show()
{
System.out.println(message);
}
}
class Student
{
private double score;
public void setScore(double score) throws ScoreException
{
if (score<0 || score>100)
throw ②;
else
③;
}
public double getScore()
{
return this.score;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Student zhangsan = new Student();
④
{
zhangsan.setScore(sc.nextDouble());
System.out.println("成績(jī)?yōu)? + zhangsan.getScore());
}catch(ScoreException e)
{
e.show();
}finally
{
⑤;
}
sc.close();
}
}
①extends Exception
②new ScoreException()
③this.score = score
④try
⑤System.out.println("程序結(jié)束")
3-6?this在構(gòu)造方法的使用
class Base
{
int x, y, z, w;
public Base(int a, int b)
{
x = a;
y = b;
}
public Base(int a, int b, int c, int d)
{
// 換一種辦法實(shí)現(xiàn)賦值x=a, y=b
①
z = c;
w = d;
}
}
public class Main
{
public static void main(String[] args)
{
Base base = ②(1, 1, 1, 1);
System.out.println(base.x + " " + base.y + " " + base.z + " " + base.w);
}
}
java構(gòu)造方法中this_Java中this關(guān)鍵字在構(gòu)造方法中的使用_瓜呼呼的博客-CSDN博客
?1、this(a,b);
2、 new Base
四、函數(shù)題
4-1?學(xué)生類(lèi)
設(shè)計(jì)一個(gè)名為StudentOf2019EE的類(lèi),表示2019電子信息工程專(zhuān)業(yè)的學(xué)生。類(lèi)包含:
- String類(lèi)型私有成員變量name,表示學(xué)生的姓名。
- int型私有成員變量money,表示學(xué)生可支配的金額(不能為負(fù)數(shù))。
- int型私有靜態(tài)(類(lèi))變量numberOfObjects,表示StudentOf2019EE對(duì)象的數(shù)量,初始值為0;每創(chuàng)建一個(gè)StudentOf2019EE對(duì)象,numberOfObjects應(yīng)當(dāng)加1。
- int型私有靜態(tài)(類(lèi))變量clazzMoney,表示全體學(xué)生繳納的班費(fèi)余額,初始值為0。
- 構(gòu)造方法StudentOf2019EE(String name),參數(shù)name用于初始化學(xué)生的姓名,可支配金額設(shè)為默認(rèn)值100。
- 構(gòu)造方法StudentOf2019EE(String name, int mongey),參數(shù)name用于初始化學(xué)生的姓名,參數(shù)money用于初始化學(xué)生的可支配金額。
- 為name和money添加getter和setter方法,都為公共方法。
- 公共的實(shí)例方法void payClazzMoney(int amount),表示從學(xué)生的可支配金額中支出數(shù)量為amount的金額作為班費(fèi)(如果學(xué)生的可支配金額<amount,則有多少交多少班費(fèi))。
- 公共的靜態(tài)方法void clazzActivity(int amount),表示班級(jí)活動(dòng),需要從班費(fèi)余額支出數(shù)量為amount的金額作為活動(dòng)經(jīng)費(fèi)(如果班費(fèi)余額<amount,則有多少支出多少)。
創(chuàng)建三個(gè)StudentOf2019EE對(duì)象,三個(gè)對(duì)象分別支付一筆錢(qián)作為班費(fèi),最后組織一次班級(jí)活動(dòng)。
裁判測(cè)試程序樣例:
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("numberOfObjects=" + StudentOf2019EE.getNumberOfObjects() + ",clazzMoney=" + StudentOf2019EE.getClazzMoney());
StudentOf2019EE a = new StudentOf2019EE("Tom");
StudentOf2019EE b = new StudentOf2019EE("Jerry", 200);
StudentOf2019EE c = a;
a.payClazzMoney(input.nextInt());
System.out.println("numberOfObjects=" + StudentOf2019EE.getNumberOfObjects() + ",clazzMoney=" + StudentOf2019EE.getClazzMoney());
b.payClazzMoney(input.nextInt());
System.out.println("numberOfObjects=" + StudentOf2019EE.getNumberOfObjects() + ",clazzMoney=" + StudentOf2019EE.getClazzMoney());
c.payClazzMoney(input.nextInt());
System.out.println("numberOfObjects=" + StudentOf2019EE.getNumberOfObjects() + ",clazzMoney=" + StudentOf2019EE.getClazzMoney());
StudentOf2019EE.clazzActivity(input.nextInt());
System.out.println("numberOfObjects=" + StudentOf2019EE.getNumberOfObjects() + ",clazzMoney=" + StudentOf2019EE.getClazzMoney());
}
}
/* 請(qǐng)?jiān)谶@里填寫(xiě)答案 */
輸入樣例:
50
300
100
400
輸出樣例:
numberOfObjects=0,clazzMoney=0
numberOfObjects=2,clazzMoney=50
numberOfObjects=2,clazzMoney=250
numberOfObjects=2,clazzMoney=300
numberOfObjects=2,clazzMoney=0
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:800 ms
內(nèi)存限制:64 MB
class StudentOf2019EE
{
private String name;
private int money;
private static int numberOfObjects = 0;
private static int clazzMoney = 0;
StudentOf2019EE(String name)
{
numberOfObjects++;
this.name = name;
money = 100;
}
StudentOf2019EE(String name, int mongey)
{
numberOfObjects++;
this.name = name;
this.money = mongey;
}
public int getMoney()
{
return money;
}
public String getName()
{
return name;
}
public void setMoney(int money)
{
this.money = money;
}
public void setName(String name)
{
this.name = name;
}
public void payClazzMoney(int amount)
{
if(this.money < amount)
{
clazzMoney += money;
money = 0;
}else
{
clazzMoney += amount;
money -= amount;
}
}
static public void clazzActivity(int amount)
{
if(clazzMoney < amount)
{
clazzMoney = 0;
}else
{
clazzMoney -= amount;
}
}
static public int getNumberOfObjects()
{
return numberOfObjects;
}
static public int getClazzMoney()
{
return clazzMoney;
}
}
?4-2?圓和圓柱體1
編寫(xiě)一個(gè)Java程序,包含類(lèi)Circle、Cylinder、Main,其中Main已經(jīng)實(shí)現(xiàn),請(qǐng)你編寫(xiě)Circle和Cylinder類(lèi)。
(1)編寫(xiě)類(lèi)Circle,表示圓形對(duì)象,包含以下成員
①屬性:radius:私有,double型,圓形半徑;
②方法:
- Circle(double radius), 構(gòu)造方法,用參數(shù)設(shè)置圓的半徑
- Circle(),構(gòu)造方法,將圓形初始化為半徑為0。
- void setRadius(double r):用參數(shù)r設(shè)置radius的值
- double getRadius():返回radius的值
- double getArea(),返回圓形的面積
- double getPerimeter(),返回圓形的周長(zhǎng)
- public String toString( ),將把當(dāng)前圓對(duì)象的轉(zhuǎn)換成字符串形式,例如圓半徑為10.0,返回字符串"Circle(r:10.0)"。
(2)編寫(xiě)一個(gè)Circle類(lèi)的子類(lèi)Cylinder,表示圓柱形對(duì)象,包含以下成員
①屬性:height:私有,double型,圓柱體高度;
②方法:
- Cylinder(double radius,double height), 構(gòu)造方法,用參數(shù)設(shè)置圓柱體的半徑和高度
- Cylinder(),構(gòu)造方法,將圓柱體的半徑和高度都初始化為0。
- void setHeight(double height):用參數(shù)height設(shè)置圓柱體的高度
- double getHeight():返回圓柱體的高度
- double getArea(),重寫(xiě)Circle類(lèi)中的area方法,返回圓柱體的表面積
- double getVolume(),返回圓柱體的體積
- public String toString( ),將把當(dāng)前圓柱體對(duì)象的轉(zhuǎn)換成字符串形式,例如半徑為10.0,高為5.0,返回字符串"Cylinder(r:10.0,h:5.0)"。
輸入格式:
第一行輸入一個(gè)整數(shù)n,表示有n個(gè)幾何圖形。
以下有n行,每行輸入一個(gè)幾何圖形的數(shù)據(jù)。
每行先輸入一個(gè)字符串表示幾何圖形的類(lèi)型,“Circle”表示圓形,“Cylinder”表示圓柱體。
如果是圓形,輸入一個(gè)浮點(diǎn)數(shù)表示其半徑。
如果是圓柱體,輸入兩個(gè)浮點(diǎn)數(shù)分別表示其半徑和高度。
輸出格式:
如果是圓形,要求計(jì)算其面積和周長(zhǎng)并輸出。
如果是圓柱體,要求計(jì)算其面積和體積并輸出。
注意,圓周率用Math.PI
裁判測(cè)試程序樣例:
輸入樣例:
4
Circle 10.0
Cylinder 10.0 10.0
Circle 1.1
Cylinder 1.1 3.4
?
輸出樣例:
The area of Circle(r:10.0) is 314.1592653589793
The perimeter of Circle(r:10.0) is 62.83185307179586
The area of Cylinder(r:10.0,h:10.0) is 1256.6370614359173
The volume of Cylinder(r:10.0,h:10.0) is 3141.5926535897934
The area of Circle(r:1.1) is 3.8013271108436504
The perimeter of Circle(r:1.1) is 6.911503837897546
The area of Cylinder(r:1.1,h:3.4) is 31.101767270538957
The volume of Cylinder(r:1.1,h:3.4) is 12.924512176868411
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
class Circle
{
private double radius;//圓形半徑
Circle(double radius)
{
this.radius = radius;
}
Circle()
{
radius = 0;
}
void setRadius(double r)
{
this.radius = r;
}
double getRadius()
{
return radius;
}
double getArea()
{
return radius * radius * Math.PI;
}
double getPerimeter()
{
return 2 * Math.PI * radius;
}
public String toString( )
{
return "Circle(r:" + radius + ")";
}
}
class Cylinder extends Circle
{
private double height;
Cylinder(double radius,double height)
{
setRadius(radius);
this.height = height;
}
Cylinder()
{
setRadius(0);
this.height = 0;
}
void setHeight(double height)
{
this.height = height;
}
double getHeight()
{
return height;
}
double getArea()
{
return 2 * getRadius() * Math.PI * getRadius() + 2 * Math.PI * getRadius() * height;
}
double getVolume()
{
return getRadius() * Math.PI * getRadius() * height;
}
public String toString( )
{
return "Cylinder(r:" + getRadius() + ",h:" + height + ")";
}
}
?4-3?根據(jù)要求,使用泛型和LinkedList編寫(xiě)StringList類(lèi),實(shí)現(xiàn)QQ號(hào)碼查找的功能。
已知數(shù)組存放一批QQ號(hào)碼,QQ號(hào)碼最長(zhǎng)為11位,最短為5位:
String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"}。
將該數(shù)組里面的所有QQ號(hào)都存放在LinkedList中,然后遍歷鏈表,將list中第一個(gè)指定長(zhǎng)度的QQ號(hào)查找出來(lái);如果不存在指定長(zhǎng)度的QQ號(hào),則輸出“not exist”。
Main類(lèi):在main方法中,調(diào)用constructList方法將strs中的字符串存入一個(gè)String的鏈表中,然后調(diào)用search方法查找第一個(gè)指定長(zhǎng)度的QQ號(hào)碼,并打印到屏幕。
編寫(xiě)StringList類(lèi),編程要求如下:
- 根據(jù)程序需求,定義成員變量、編寫(xiě)構(gòu)造方法。
- LinkedList constructList(String[] strs) 方法:將String數(shù)組strs中的元素添加到鏈表中,構(gòu)建一個(gè)String對(duì)象鏈表,最后返回鏈表。
- String search(LinkedList list)方法:使用scanner的nextInt()方法從鍵盤(pán)讀入一個(gè)int,表示指定長(zhǎng)度,然后遍歷鏈表,查找出鏈表中第一個(gè)指定長(zhǎng)度的QQ號(hào)碼并返回;如果不存在指定長(zhǎng)度的QQ號(hào),則返回字符串"not exist"。
函數(shù)接口定義:
LinkedList<String> constructList(String[] strs);
String search(LinkedList<String> list);
裁判測(cè)試程序樣例:
import java.util.*;
public class Main {
public static void main(String[] args) {
String[] strs = {"12345","67891","12347809931","98765432102","67891","12347809933"};
StringList sl=new StringList();
LinkedList<String> qqList=sl.constructList(strs);
System.out.println(sl.search(qqList));
}
}
/* 請(qǐng)?jiān)谶@里填寫(xiě)答案:StringList類(lèi) */
輸入樣例:
在這里給出一組輸入。例如:
5
?
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
12345
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
Java 建立一個(gè)單鏈表(增刪查改)_愛(ài)干飯的猿的博客-CSDN博客_java new一個(gè)鏈表
class StringList
{
StringList()
{}
LinkedList<String> constructList(String[] strs)
{
LinkedList<String> list = new LinkedList<String>();
for(int i=0; i< strs.length;i++)
{
list.add(strs[i]);
}
return list;
}
String search(LinkedList<String> list)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
Iterator it = list.iterator();
while (it.hasNext())
{
String str = (String) it.next();
if(a == str.length())
{
return str;
}
}
return "not exist";
}
}
?簡(jiǎn)化了一下下面的代碼2021-06-11_dada dog的博客-CSDN博客_編寫(xiě)一個(gè)程序,其中提供linkedlist鏈表,用來(lái)保存班級(jí)同學(xué)的qq號(hào),假設(shè)qq號(hào)都是8位數(shù)
String str = it.next();如果不在it前面加(String)會(huì)顯示錯(cuò)誤ava: 不兼容的類(lèi)型: java.lang.Object無(wú)法轉(zhuǎn)換為java.lang.String
4-4??jmu-Java-06異常-finally
代碼中向系統(tǒng)申請(qǐng)資源,到最后都要將資源釋放。
現(xiàn)有一Resource
類(lèi)代表資源類(lèi),包含方法:
-
open(String str)
打開(kāi)資源,聲明為拋出Exception
(包含出錯(cuò)信息)。 -
close()方法
釋放資源,聲明為拋出RuntimeException
(包含出錯(cuò)信息)
現(xiàn)在根據(jù)open(String str)
中str的不同,打印不同的信息。str的內(nèi)容分為4種情況:
-
fail fail
,代表open和close均會(huì)出現(xiàn)異常。打印open的出錯(cuò)信息與close的出錯(cuò)信息。 -
fail success
,代表open拋出異常,打印open出錯(cuò)信息。close正常執(zhí)行,打印resource release success
-
success fail?
,代表open正常執(zhí)行,打印resource open success
。close拋出異常,打印close出錯(cuò)信息。 -
success success
,代表open正常執(zhí)行,打印resource open success
,close正常執(zhí)行打印resource release success
。
注1:你不用編寫(xiě)打印出錯(cuò)信息的代碼。
注2:捕獲異常后使用System.out.println(e)
輸出異常信息,e是所產(chǎn)生的異常。
裁判測(cè)試程序:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Resource resource = null;
try{
resource = new Resource();
resource.open(sc.nextLine());
/*這里放置你的答案*/
sc.close();
}
以下輸入樣例代表輸入success success
。
輸入樣例
success success
輸出樣例
resource open success
resource release success
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
除了題目要我們寫(xiě)的代碼,這個(gè)代碼還有其他地方也要補(bǔ)一下
miao
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Resource resource = null;
try {
resource = new Resource();
resource.open(sc.nextLine());
/*這里放置你的答案*/
System.out.println("resource open success");
}catch (Exception e)
{
System.out.println(e);
}
try
{
sc.close();
System.out.println("resource release success");
}catch(RuntimeException e)
{
System.out.println(e);
}
}
}
class Resource
{
public open(String s)
{
}
}
?4-5?Java-函數(shù)題-1(字符串查詢(xún))
本題要求實(shí)現(xiàn)一個(gè)函數(shù),可統(tǒng)計(jì)任一字符串中某個(gè)字符出現(xiàn)的次數(shù)。例如?abca?中,a?出現(xiàn)了?2?次,b?出現(xiàn)了?1?次。
函數(shù)接口定義:
函數(shù)的原型如下:
public static int countChar(String string, char c);
?
其中?string
?和?c
?都是用戶傳入的參數(shù)。
string
?的長(zhǎng)度在區(qū)間?[1,1000]?以?xún)?nèi);?c
?是一個(gè)可能出現(xiàn)在字符串中的字符。函數(shù)須返回?string
?中?c
?出現(xiàn)的次數(shù)。
裁判測(cè)試程序樣例:
在這里給出函數(shù)被調(diào)用進(jìn)行測(cè)試的例子。例如:
import java.util.*;
public class Main {
/* 此區(qū)間是要編寫(xiě)的函數(shù) */
public static int countChar(String string, char c) {
//請(qǐng)補(bǔ)充完整
}
/* 此區(qū)間是要編寫(xiě)的函數(shù) */
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
String s1 = key.nextLine();
String s2 = key.nextLine();
System.out.println(countChar(s1, s2.charAt(0)));
}
}
輸入樣例:
在這里給出一組輸入。例如:
abca
a
?
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
2
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
?這題有毛病,還得加上函數(shù)名才正確,但題目中不是給了嗎,難搞、
Java基礎(chǔ)---字符串之如何比較字符串_Tan.]der的博客-CSDN博客_字符和字符串怎么比較
Java中字符串比較/字符比較_NJUSTZJC的博客-CSDN博客
注意分清字符數(shù)組與字符串的區(qū)別
public static int countChar(String string, char c)
{
//請(qǐng)補(bǔ)充完整
int a = 0;
for(int i=0;i<string.length();i++)
{
if(c == string.charAt(i))
a++;
}
return a;
}
4-6?數(shù)組求和
編寫(xiě)程序。讀入用戶輸入的10個(gè)整數(shù)存入數(shù)組中,并對(duì)數(shù)組求和。
要求實(shí)現(xiàn)3個(gè)數(shù)組求和方法。
//求數(shù)組a中所有元素的和
static int sum(int[] a){
}
//求數(shù)組a中下標(biāo)從start開(kāi)始到數(shù)組末尾的元素的和
static int sum(int[] a, int start){
}
//求數(shù)組a中下標(biāo)從start開(kāi)始到end-1的元素的和
static int sum(int[] a, int start, int end){
}
裁判測(cè)試程序樣例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] a = new int[10];
int start,end;
for(int i = 0;i < a.length; i++){
a[i] = input.nextInt();
}
System.out.println(sum(a));
start = input.nextInt();
System.out.println(sum(a, start));
start = input.nextInt();
end = input.nextInt();
System.out.println(sum(a, start, end));
}
/* 請(qǐng)?jiān)谶@里填寫(xiě)答案 */
}
輸入樣例:
1 2 3 4 5 6 7 8 9 10
5
5 8
?
輸出樣例:
55
40
21
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
?PTA非零返回的解決辦法java_兩條魚(yú)h的博客-CSDN博客_非零返回
第一次非零返回
static int sum(int[] a)
{
int sum = 0;
for(int i=0;i<a.length;i++)
{
sum += a[i];
}
return sum;
}
//求數(shù)組a中下標(biāo)從start開(kāi)始到數(shù)組末尾的元素的和
static int sum(int[] a, int start)
{
int sum = 0;
for(int i = start; i<a.length;i++)
{
sum += a[i];
}
return sum;
}
//求數(shù)組a中下標(biāo)從start開(kāi)始到end-1的元素的和
static int sum(int[] a, int start, int end)
{
int sum = 0;
for(int i =start;i<end;i++)
{
sum += a[i];
}
return sum;
}
?第二次對(duì)了
static int sum(int[] a)
{
int sum = 0;
int c = a.length;
for(int i=0;i<c;i++)
{
int b = a[i];
sum += b;
}
return sum;
}
//求數(shù)組a中下標(biāo)從start開(kāi)始到數(shù)組末尾的元素的和
static int sum(int[] a, int start)
{
int sum = 0;
int c = a.length;
int d = 0;
if(start >= 0)
d = start;
for(int i = d; i<c;i++)
{
int b = a[i];
sum += b;
}
return sum;
}
//求數(shù)組a中下標(biāo)從start開(kāi)始到end-1的元素的和
static int sum(int[] a, int start, int end)
{
int sum = 0;
int c = a.length;
int d = 0;
if(start >= 0)
d = start;
for(int i = d;i<=end-1&&i<c;i++)
{
int b = a[i];
sum += b;
}
return sum;
}
? ? ? ? ?原來(lái)返回非零是指數(shù)組越界了
五、編程題
5-1??sdut-最大公約數(shù)和最小公倍數(shù)
給定2個(gè)正整數(shù),求它們的最大公約數(shù)和最小公倍數(shù),并輸出。
輸入格式:
輸入有若干組。
每組數(shù)據(jù),在一行中給出兩個(gè)正整數(shù)M和N(≤1000),中間有1個(gè)空格。
不是有若干組測(cè)試樣例,而是一組測(cè)試樣例里面有若干組輸入;
輸出格式:
對(duì)于每組輸入,在一行中順序輸出M和N的最大公約數(shù)和最小公倍數(shù),兩數(shù)字間以1個(gè)空格分隔。
輸入樣例:
18 12
20 15
39 26
5 76
45 25
1993 343
?
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
6 36
5 60
13 78
1 380
5 225
1 683599
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
first提交
偷了下懶,結(jié)果有時(shí)間限制
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt())
{
int a = sc.nextInt();
int b = sc.nextInt();
int min,max;
if(a >= b)
{
min =a;
max = b;
}else
{
min = b;
max = a;
}
for(;max>1;max--)
{
if(a%max==0&&b%max==0)
break;;
}
for(;;min++)
{
if(min%a==0&&min%b==0)
break;;
}
System.out.println(max+" "+min);
}
}
}
最后提交
C++之 最大公約數(shù)求法_ililily的博客-CSDN博客_c++最大公約數(shù)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt())
{
int a = sc.nextInt();
int b = sc.nextInt();
int c = a;
int d = b;
int min,max;
int r = a % b;
while (r!=0)
{
a = b;
b = r;
r = a % b;
}
min = (c/b) * (d/b) * b;
System.out.println(b+" "+min);
}
}
}
5-2?java編程判斷斐波那契數(shù)是質(zhì)數(shù)
斐波那契數(shù)列(FibonacciSequence),又稱(chēng)黃金分割數(shù)列。因數(shù)學(xué)家列昂納多·斐波那契(LeonardoFibonacci)以兔子繁殖為例子而引入,故又稱(chēng)為“兔子數(shù)列”,指的是這樣一個(gè)數(shù)列:1,?1,?2,?3,?5,?8,?13,?21,?34??
在數(shù)學(xué)上,斐波那契數(shù)列被以如下的遞推形式定義:
F(1)=1,F(2)=1
F(n)=F(n?1)+F(n?2),(n≥3,n∈N)
素?cái)?shù)也稱(chēng)為質(zhì)數(shù),是指在大于?1?的整數(shù)中,只能被?1?和其自身整除的數(shù),2?是最小的質(zhì)數(shù)。
我們想要知道斐波那契數(shù)列的第?n?項(xiàng)是否是一個(gè)素?cái)?shù),請(qǐng)你編寫(xiě)程序完成判斷。
輸入格式:
一行,一個(gè)整數(shù)?n(1≤n≤50)
輸出格式:
一行,一個(gè)單詞,如果?F(n)?是素?cái)?shù),輸出
true
,反之輸出false
輸入樣例1:
1
?
輸出樣例1:
false
?
輸入樣例2:
2
?
輸出樣例2:
false
?
輸入樣例3:
3
?
輸出樣例3:
true
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:2000 ms
內(nèi)存限制:64 MB
?第一次只對(duì)了一個(gè),而且題目中有錯(cuò)誤,2不是素?cái)?shù),但題目把他歸為素?cái)?shù);
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int num = feb(a);
if(num == 2)
{
System.out.println("false");
}else if(num % 2 == 0)
{
System.out.println("true");
}else
{
double b = Math.sqrt(num);
for (int i = 3;i<b;i=i+2)
{
if(num % i == 0)
{
num = 0;
break;
}
}
if(num == 0)
{
System.out.println("true");
}else
System.out.println("false");
}
}
public static int feb(int i)
{
// 第一種情況,前兩個(gè)默認(rèn)的數(shù)字 均為1。
if(i == 1 || i == 2){
return 1;
}else
{
// 第二種情況,返回前兩個(gè)數(shù)之和
return feb(i - 1) + feb(i - 2);
}
}
}
?1、大于五十的數(shù)應(yīng)該返回零
2、用遞歸的方式求斐波那契數(shù)列會(huì)耗費(fèi)時(shí)間
3、這個(gè)是判斷是否是素?cái)?shù),判斷結(jié)果寫(xiě)反了
最后一次提交
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int num = 0;
int u = 0, k = 1;
if(a<=50&&a>=1)
{
for(int i=1;i<=a;i++)
{
u = num;
num = k + num;
k = u;
}
if(num == 2)
{
System.out.println("true");
}else if(num % 2 == 0)
{
System.out.println("false");
}else
{
double b = Math.sqrt(num);
for (int i = 3;i<b;i=i+2)
{
if(num % i == 0)
{
num = 0;
break;
}
}
if(num == 0)
{
System.out.println("false");
}else
System.out.println("true");
}
}
}
}
5-3?統(tǒng)計(jì)學(xué)生年齡異常的人數(shù)
定義Student類(lèi):
(1)成員變量有:姓名,年齡。
(2)對(duì)成員變量進(jìn)行封裝。
(3)定義getXXXX,setXXXX方法,其中對(duì)年齡的限定條件是:年齡大于0。
?
定義主類(lèi),包含主方法:
實(shí)現(xiàn)輸入5個(gè)學(xué)生,輸出年齡不符合要求 的學(xué)生人數(shù)和姓名。
如果年齡全部正確,輸出“right”,如果全部錯(cuò)誤,輸出"all wrong"。
?
輸入格式:
5行,每行1個(gè)學(xué)生信息,包括姓名和年齡
輸出格式:
多行,第1行是不符合要求的人數(shù)
其余各行是不符合要求的學(xué)生的姓名
如果年齡全部正確,輸出“right”,如果全部錯(cuò)誤,輸出"all wrong"。
輸入樣例:
zhang 18
Li -15
wang 0
zhao 20
wu -20
?
輸出樣例:
3
Li
wang
wu
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String a[] = new String[5];
int b[] = new int[5];
for(int i = 0;i < 5;i++)
{
a[i] = sc.next();
b[i] = sc.nextInt();
Student stu = new Student(a[i],b[i]);
}
if(Student.num == 0)
{
System.out.println("right");
}
else if(Student.num == 5)
{
System.out.println("all wrong");
}
else
{
System.out.println(Student.num);
for(int j = 0;j < 5;j++)
{
if(b[j]<=0)
System.out.println(a[j]);
}
}
}
}
class Student
{
private String name;
private int age;
static int num = 0;
//成員
public Student() {}
public Student(String name,int age)
{
this.name=name;
this.age=age;
if(age <= 0)
num++;
}
//構(gòu)造
public String getname()
{
return this.name;
}
public int getage()
{
return this.age;
}
public void setname(String name)
{
this.name = name;
}
public void setage(int age)
{
this.age = age;
}//get set
}
?5-4?jmu-java-隨機(jī)數(shù)-使用蒙特卡羅法計(jì)算圓周率的值
嘗試使用蒙特卡羅法計(jì)算圓周率(π)的值。原理如下:
以原點(diǎn)(0, 0)作為圓心,半徑為1畫(huà)一個(gè)圓。該圓的外切正方形,邊長(zhǎng)為2。
現(xiàn)往該正方形內(nèi)隨機(jī)投點(diǎn),數(shù)量足夠多的情況下,落入圓內(nèi)的點(diǎn)與落入整個(gè)
外切正方形的點(diǎn)的數(shù)量比值大概為:?4?r2π?r2?,然后就可以得到π的值。
注意
- 請(qǐng)使用jdk庫(kù)中的Random對(duì)象來(lái)生成隨機(jī)數(shù)。
- 使用Math類(lèi)中的sqrt與pow函數(shù)來(lái)計(jì)算開(kāi)根號(hào)與平方值。
- 讓點(diǎn)(x,y)投在整個(gè)矩形中,x與y的取值范圍為(-1≤x<1, -1≤y<1)。
輸入格式:
隨機(jī)數(shù)種子seed 投點(diǎn)個(gè)數(shù)n
注意:seed為long型,n為int型
輸出格式:
計(jì)算出的值圓周率的值
輸入樣例:
2 100000
?
輸出樣例:
3.14684
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
import java.util.Random;
import java.util.Scanner;
/**
*
* @author 梓葉楓林
* @date 2020/10/28
*/
public class Main {
public static void main (String [] args) {
Scanner scanner = new Scanner(System.in);
long seed = scanner.nextLong();
int n = scanner.nextInt();
scanner.close();
//將隨機(jī)數(shù)種子放入隨機(jī)數(shù)中
Random random = new Random(seed);
int insideNum = 0;
for (int i = 0; i < n; i++) {
//random.nextDouble()的值域[0.0, 1.0)。要使函數(shù)為[-1.0, 1.0),所以進(jìn)行了下面的操作。
double x = random.nextDouble() *2 - 1;
double y = random.nextDouble() *2 - 1;
//記錄點(diǎn)在圓內(nèi)的數(shù)量
if (Math.pow(x, 2) + Math.pow(y, 2) <= 1) {
insideNum++;
}
}
//從所給的公式 反推出PI的公式
//需要把insideNum強(qiáng)轉(zhuǎn)成double,不然整數(shù)相除會(huì)出錯(cuò)
System.out.println(4 * ((double) insideNum / n));
}
}
?5-5?定義學(xué)生類(lèi)覆蓋Object中的方法實(shí)現(xiàn)Comparable接口
定義一個(gè)學(xué)生類(lèi)Student,成員變量包括:姓名,生日,學(xué)號(hào),學(xué)校;重寫(xiě)方法toString,equals,hashCode;實(shí)現(xiàn)接口Comparable,按照學(xué)號(hào)大小進(jìn)行比較;定義構(gòu)造方法。
代碼形式如下:
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int num=in.nextInt();
Student[] studentArray = new Student[num];
for(int i=0;i<num;i++)
{
String name=in.next();
int year=in.nextInt();
int month=in.nextInt();
int day=in.nextInt();
String studentId=in.next();
String school=in.next();
studentArray[i]=new Student(name,year,month,day,studentId,school);
}
Arrays.sort(studentArray);
for(Student s:studentArray)
System.out.println(s);
}
}
class Student implements Comparable
{
//給出Student的定義
}
輸入格式:
第一行輸入學(xué)生人數(shù)。其他各行每行輸入一個(gè)學(xué)生的姓名,出生年月日,學(xué)號(hào),學(xué)號(hào),用空格分隔。
輸出格式:
按照學(xué)號(hào)從小到大排序的學(xué)生信息,每個(gè)學(xué)生信息一行。
輸入樣例:
例如:
3
李翔 2002 10 9 202019001 ?北京化工大學(xué)
張凱 2002 11 23 202019015 ?北京化工大學(xué)
汪海 2002 7 5 202019012 ?北京化工大學(xué)
輸出樣例:
例如:
Student[name=李翔, birthday=2002-10-09, studentId=202019001, school=北京化工大學(xué)]
Student[name=汪海, birthday=2002-07-05, studentId=202019012, school=北京化工大學(xué)]
Student[name=張凱, birthday=2002-11-23, studentId=202019015, school=北京化工大學(xué)]
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
https://blog.csdn.net/hfut_why/article/details/82929206
https://blog.csdn.net/weixin_45503113/article/details/99124988
https://blog.csdn.net/GavinLi2588/article/details/79688452
https://blog.csdn.net/wusunshine/article/details/11917479
https://blog.csdn.net/m0_47384542/article/details/124093498
Java中在數(shù)字前自動(dòng)補(bǔ)零方法_會(huì)跑的葫蘆怪的博客-CSDN博客_java自動(dòng)補(bǔ)0
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int num=in.nextInt();
Student[] studentArray = new Student[num];
for(int i=0;i<num;i++)
{
String name=in.next();
int year=in.nextInt();
int month=in.nextInt();
int day=in.nextInt();
String studentId=in.next();
String school=in.next();
studentArray[i]=new Student(name,year,month,day,studentId,school);
}
Arrays.sort(studentArray);
for(Student s:studentArray)
System.out.println(s);
}
}
class Student implements Comparable<Student>
{
private String name,sch,stunum;
private int year,month,day;
// @Override
// public boolean equals
// {
//
// }
Student(String name,int year,int month,int day, String studentId, String school)
{
this.name = name;
this.year = year;
this.month = month;
this.day = day;
this.stunum = studentId;
this.sch = school;
}
@Override
public int hashCode()
{
return 1;
}
@Override
public String toString()
{
return ("Student[name="+name+", birthday="+year+"-"+String.format("S02d",month)+"-"+String.format("S02d",day)+", studentId="+stunum+", school="+sch+"]");
}
@Override
public int compareTo(Student s)
{
int flag = 0;
for(int i=0;i<s.stunum.length();i++)
{
if(stunum.charAt(i)>s.stunum.charAt(i))
{
flag = 1;
break;
}
else if(stunum.charAt(i)<s.stunum.charAt(i))
{
flag = -1;
break;
}
}
return flag == 1 ? 1 : -1;
}
}
5-6?十六進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù)
輸入一個(gè)十六進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù)輸出。
輸入格式:
輸入一個(gè)十六進(jìn)制數(shù)
輸出格式:
輸出一個(gè)十進(jìn)制數(shù)
輸入樣例1:
A
?
輸出樣例1:
10
?
輸入樣例2:
ffff
?
輸出樣例2:
65535
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
import java.util.Scanner;
//進(jìn)制轉(zhuǎn)換
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String x = sc.nextLine();
long a = Long.parseLong(x,16); //16進(jìn)制轉(zhuǎn)10進(jìn)制
System.out.println(a);
}
}
?7-7?List的使用
本題練習(xí)列表的使用。
- 定義Person類(lèi)
- 定義私有屬性String name,int age,使用Eclipse生成每個(gè)屬性setter 、getter,有參Person(String name,int age) 、無(wú)參 構(gòu)造方法,toString方法。
- 定義Main類(lèi),在main方法中
- 定義List list = new ArrayList();
- 用鍵盤(pán)給變量n賦值
- 生成n個(gè)Person對(duì)象并添加到列表中,該P(yáng)erson的name和age通過(guò)鍵盤(pán)給出
- 循環(huán)列表,輸出列表所有Person對(duì)象信息(調(diào)用toString方法)
- 輸入一個(gè)字符串表示姓名,判斷該字符串表示的Person對(duì)象在List中是否存在,如果存在,輸出該P(yáng)erson,否則輸出此人不存在。
輸入格式:
先一行輸入n表示對(duì)象個(gè)數(shù),然后每行輸入一個(gè)Person對(duì)象的name和age
一行輸入一個(gè)人的姓名對(duì)其進(jìn)行查詢(xún)
輸出格式:
對(duì)每一對(duì)象,在一行中輸出對(duì)象的信息。
對(duì)查詢(xún)的人員,查到輸出該人的信息,否則輸出此人不存在。
輸入樣例:
在這里給出一組輸入。例如:
3
zhang 23
li 44
wang 33
li
?
3
zhang 23
li 44
wang 33
may
?
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
Person [name=li, age=44]
?
Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
此人不存在
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Person>list=new ArrayList<>();
Scanner in=new Scanner(System.in);
int n=in.nextInt();
for(int i=0;i<n;i++) {
Person p=new Person(in.next(),in.nextInt());
list.add(p);
System.out.println(p.toString());
}
String l=in.next();
int i;
for(i=0;i<list.size();i++) {
if(list.get(i).getName().equals(l)) {
System.out.println(list.get(i).toString());
break;
}
}
if(i==list.size()) {
System.out.println("此人不存在");
}
}
}
class Person{
private String name;
private int age;
public Person(String name,int age) {
this.age=age;
this.name=name;
}
public Person() {
this.age=age;
this.name=name;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + 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;
}
}
?5-8??jmu-Java-05集合(泛型)-10-GeneralStack
以前定義的IntegerStack接口,只能用于存放Integer類(lèi)型的數(shù)據(jù)。然而對(duì)于棧來(lái)說(shuō),不管內(nèi)部存放的是什么類(lèi)型的數(shù)據(jù),基本操作與元素的具體類(lèi)型無(wú)關(guān)。
1. 編寫(xiě)一個(gè)通用的GeneralStack接口,接口中的操作對(duì)任何引用類(lèi)型的數(shù)據(jù)都適用。
一旦定義完畢,只能存放一種類(lèi)型的數(shù)據(jù),比如只能存放String或只能存放Integer。GeneralStack
接口方法如下:
push(item); //如item為null,則不入棧直接返回null。
pop(); //出棧,如為棧為空,則返回null。
peek(); //獲得棧頂元素,如為空,則返回null.
public boolean empty();//如為空返回true
public int size(); //返回棧中元素?cái)?shù)量
2.定義GeneralStack的實(shí)現(xiàn)類(lèi)ArrayListGeneralStack
內(nèi)部使用ArrayList對(duì)象存儲(chǔ),屬性名為list
。
方法:public String toString()
//該方法的代碼為return list.toString();
提示:
- 不用使用top指針。
- 直接復(fù)用ArrayList中已有的方法。
- pop時(shí)應(yīng)將相應(yīng)的元素從ArrayList中移除。
- 代碼中不要出現(xiàn)類(lèi)型不安全的強(qiáng)制轉(zhuǎn)換。
3.定義Car對(duì)象
屬性:
private int id;
private String name;
?
方法:Eclipse自動(dòng)生成setter/getter,toString方法。
4.main方法說(shuō)明
- 輸入選項(xiàng),有
quit, Integer, Double, Car
4個(gè)選項(xiàng)。如果輸入quit,則直接退出。否則,輸入整數(shù)m與n。m代表入棧個(gè)數(shù),n代表出棧個(gè)數(shù)。然后聲明棧變量stack
。- 輸入
Integer
,打印Integer Test
。建立可以存放Integer類(lèi)型的ArrayListGeneralStack
。入棧m次,出棧n次。打印棧的toString方法。最后將棧中剩余元素出棧并累加輸出。- 輸入
Double
?,打印Double Test
。剩下的與輸入Integer一樣。- 輸入
Car
,打印Car Test
。其他操作與Integer、Double基本一樣。只不過(guò)最后將棧中元素出棧,并將其name依次輸出。
2、3、4步驟做完都要使用代碼System.out.println(stack.getClass().getInterfaces()[0]);
打印標(biāo)識(shí)信息
特別注意:如果棧為空的時(shí)候繼續(xù)出棧,則返回null
輸入樣例
Integer
5
2
1 2 3 4 5
Double
5
3
1.1 2.0 4.9 5.7 7.2
Car
3
2
1 Ford
2 Cherry
3 BYD
quit
?
輸出樣例
Integer Test
push:1
push:2
push:3
push:4
push:5
pop:5
pop:4
[1, 2, 3]
sum=6
interface GeneralStack
Double Test
push:1.1
push:2.0
push:4.9
push:5.7
push:7.2
pop:7.2
pop:5.7
pop:4.9
[1.1, 2.0]
sum=3.1
interface GeneralStack
Car Test
push:Car [id=1, name=Ford]
push:Car [id=2, name=Cherry]
push:Car [id=3, name=BYD]
pop:Car [id=3, name=BYD]
pop:Car [id=2, name=Cherry]
[Car [id=1, name=Ford]]
Ford
interface GeneralStack
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
import java.util.ArrayList;
import java.util.Scanner;
interface GeneralStack<T>{
public T push(T item); //如item為null,則不入棧直接返回null。
public T pop(); //出棧,如為棧為空,則返回null。
public T peek(); //獲得棧頂元素,如為空,則返回null.
public boolean empty();//如為空返回true
public int size(); //返回棧中元素?cái)?shù)量
}
class ArrayListGeneralStack implements GeneralStack{
ArrayList list=new ArrayList();
@Override
public String toString() {
return list.toString();
}
@Override
public Object push(Object item) {
if (list.add(item)){
return item;
}else {
return false;
}
}
@Override
public Object pop() {
if (list.size()==0){
return null;
}
return list.remove(list.size()-1);
}
@Override
public Object peek() {
return list.get(list.size()-1);
}
@Override
public boolean empty() {
if (list.size()==0){
return true;
}else {
return false;
}
}
@Override
public int size() {
return list.size();
}
}
class Car{
private int id;
private String name;
@Override
public String toString() {
return "Car [" +
"id=" + id +
", name=" + name +
']';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car(int id, String name) {
this.id = id;
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (true){
String s=sc.nextLine();
if (s.equals("Double")){
System.out.println("Double Test");
int count=sc.nextInt();
int pop_time=sc.nextInt();
ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
for (int i=0;i<count;i++){
System.out.println("push:"+arrayListGeneralStack.push(sc.nextDouble()));
}
for (int i=0;i<pop_time;i++){
System.out.println("pop:"+arrayListGeneralStack.pop());
}
System.out.println(arrayListGeneralStack.toString());
double sum=0;
int size=arrayListGeneralStack.size();
for (int i=0;i<size;i++){
sum+=(double)arrayListGeneralStack.pop();
}
System.out.println("sum="+sum);
System.out.println("interface GeneralStack");
}else if (s.equals("Integer")){
System.out.println("Integer Test");
int count=sc.nextInt();
int pop_time=sc.nextInt();
ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
for (int i=0;i<count;i++){
System.out.println("push:"+arrayListGeneralStack.push(sc.nextInt()));
}
for (int i=0;i<pop_time;i++){
System.out.println("pop:"+arrayListGeneralStack.pop());
}
System.out.println(arrayListGeneralStack.toString());
int sum=0;
int size=arrayListGeneralStack.size();
for (int i=0;i<size;i++){
sum+=(int)arrayListGeneralStack.pop();
}
System.out.println("sum="+sum);
System.out.println("interface GeneralStack");
}else if (s.equals("Car")){
System.out.println("Car Test");
int count=sc.nextInt();
int pop_time=sc.nextInt();
ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
for (int i=0;i<count;i++){
int id=sc.nextInt();
String name=sc.next();
Car car = new Car(id,name);
System.out.println("push:"+arrayListGeneralStack.push(car));
}
for (int i=0;i<pop_time;i++){
System.out.println("pop:"+arrayListGeneralStack.pop());
}
System.out.println(arrayListGeneralStack.toString());
if (arrayListGeneralStack.size()>0){
int size=arrayListGeneralStack.size();
for (int i=0;i<size;i++){
Car car=(Car) arrayListGeneralStack.pop();
System.out.println(car.getName());
}
}
System.out.println("interface GeneralStack");
}else if (s.equals("quit")){
break;
}
}
}
}
5-9 復(fù)制源碼
編寫(xiě)程序,將程序文件的源代碼復(fù)制到程序文件所在目錄下的“temp.txt”文件中。
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
5-10?設(shè)計(jì)一個(gè)銀行業(yè)務(wù)類(lèi)
編寫(xiě)一個(gè)銀行業(yè)務(wù)類(lèi)BankBusiness,具有以下屬性和方法:
(1)公有、靜態(tài)的屬性:銀行名稱(chēng)bankName,初始值為“中國(guó)銀行”。
(2)私有屬性:賬戶名name、密碼password、賬戶余額balance。
(3)銀行對(duì)用戶到來(lái)的歡迎(welcome)動(dòng)作(靜態(tài)、公有方法),顯示“中國(guó)銀行歡迎您的到來(lái)!”,其中“中國(guó)銀行”自動(dòng)使用bankName的值。
(4)銀行對(duì)用戶離開(kāi)的提醒(welcomeNext)動(dòng)作(靜態(tài)、公有方法),顯示“請(qǐng)收好您的證件和物品,歡迎您下次光臨!”
(5)帶參數(shù)的構(gòu)造方法,完成開(kāi)戶操作。需要賬戶名name、密碼password信息,同時(shí)讓賬戶余額為0。
(6)用戶的存款(deposit)操作(公有方法,需要密碼和交易額信息),密碼不對(duì)時(shí)無(wú)法存款且提示“您的密碼錯(cuò)誤!”;密碼正確、完成用戶存款操作后,要提示用戶的賬戶余額,例如“您的余額有1000.0元?!?。
(7)用戶的取款(withdraw)操作(公有方法,需要密碼和交易額信息)。密碼不對(duì)時(shí)無(wú)法取款且提示“您的密碼錯(cuò)誤!”;密碼正確但余額不足時(shí)提示“您的余額不足!”;密碼正確且余額充足時(shí)扣除交易額并提示用戶的賬戶余額,例如“請(qǐng)取走鈔票,您的余額還有500.0元?!?。
編寫(xiě)一個(gè)測(cè)試類(lèi)Main,在main方法中,先后執(zhí)行以下操作:
(1)調(diào)用BankBusiness類(lèi)的welcome()方法。
(2)接收鍵盤(pán)輸入的用戶名、密碼信息作為參數(shù),調(diào)用BankBusiness類(lèi)帶參數(shù)的構(gòu)造方法,從而創(chuàng)建一個(gè)BankBusiness類(lèi)的對(duì)象account。
(3)調(diào)用account的存款方法,輸入正確的密碼,存入若干元。密碼及存款金額從鍵盤(pán)輸入。
(4)調(diào)用account的取款方法,輸入錯(cuò)誤的密碼,試圖取款若干元。密碼及取款金額從鍵盤(pán)輸入。
(5)調(diào)用account的取款方法,輸入正確的密碼,試圖取款若干元(取款金額大于余額)。密碼及取款金額從鍵盤(pán)輸入。
(6)調(diào)用account的取款方法,輸入正確的密碼,試圖取款若干元(取款金額小于余額)。密碼及取款金額從鍵盤(pán)輸入。
(7)調(diào)用BankBusiness類(lèi)的welcomeNext()方法。
輸入格式:
輸入開(kāi)戶需要的姓名、密碼
輸入正確密碼、存款金額
輸入錯(cuò)誤密碼、取款金額
輸入正確密碼、大于余額的取款金額
輸入正確密碼、小于余額的取款金額
輸出格式:
中國(guó)銀行(銀行名稱(chēng))歡迎您的到來(lái)!
您的余額有多少元。
您的密碼錯(cuò)誤!
您的余額不足!
請(qǐng)取走鈔票,您的余額還有多少元。
請(qǐng)收好您的證件和物品,歡迎您下次光臨!
輸入樣例:
在這里給出一組輸入。請(qǐng)注意,輸入與輸出是交替的,具體順序請(qǐng)看測(cè)試類(lèi)中的說(shuō)明。例如:
張三 123456
123456 1000
654321 2000
123456 2000
123456 500
?
輸出樣例:
在這里給出相應(yīng)的輸出。請(qǐng)注意,輸入與輸出是交替的,具體順序請(qǐng)看測(cè)試類(lèi)中的說(shuō)明。例如:
中國(guó)銀行歡迎您的到來(lái)!
您的余額有1000.0元。
您的密碼錯(cuò)誤!
您的余額不足!
請(qǐng)取走鈔票,您的余額還有500.0元。
請(qǐng)收好您的證件和物品,歡迎您下次光臨!
?
代碼長(zhǎng)度限制:16 KB
時(shí)間限制:400 ms
內(nèi)存限制:64 MB
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BankBusiness.welcome();
Scanner in = new Scanner(System.in);
BankBusiness user = new BankBusiness(in.next(), in.next());
user.deposit(in.next(), in.nextDouble());
user.withdraw(in.next(), in.nextDouble());
user.withdraw(in.next(), in.nextDouble());
user.withdraw(in.next(), in.nextDouble());
BankBusiness.welcomeNext();
}
}
class BankBusiness {
public static String bankName = "中國(guó)銀行";
private String name,password;
private double balance;
public static void welcome() {
System.out.println(bankName+"歡迎您的到來(lái)!");
}
public static void welcomeNext() {
System.out.println("請(qǐng)收好您的證件和物品,歡迎您下次光臨!");
}
public BankBusiness(String name, String password) {
super();
this.name = name;
this.password = password;
this.balance = 0;
}
public void deposit(String password,double change) {
if(!password.equals(this.password)) {
System.out.println("您的密碼錯(cuò)誤!");
return;
}
this.balance += change;
System.out.println("您的余額有"+this.balance+"元。");
}
public void withdraw(String password,double change) {
if(!password.equals(this.password)) {
System.out.println("您的密碼錯(cuò)誤!");
return;
}
if(this.balance<change) {
System.out.println("您的余額不足!");
return;
}
this.balance -= change;
System.out.println("請(qǐng)取走鈔票,您的余額還有"+this.balance+"元。");
}
}
一、判斷題補(bǔ)充
1-1 所謂方法重寫(xiě),就是在同一個(gè)作用域內(nèi)方法名相同但參數(shù)個(gè)數(shù)或者參數(shù)類(lèi)型不同的方法。
F
1-2?break語(yǔ)句只用于循環(huán)語(yǔ)句中,它的作用是跳出循環(huán)。
F
還作用于switch語(yǔ)句
1-3?Java語(yǔ)言不區(qū)分大小寫(xiě)
F
1-4?Java的Object類(lèi)沒(méi)有父類(lèi)。
T
1-5?在Animal類(lèi)和其子類(lèi)Dog類(lèi)中均定義了一個(gè)名為age的整形變量,則按照多態(tài)原理,下列程序中訪問(wèn)的是Dog類(lèi)中的age變量。
Animal a=new Dog();
System.out.println(a.age);
F
多態(tài):調(diào)用屬性時(shí)會(huì)向父類(lèi)索引 父類(lèi)子類(lèi)都有有則調(diào)用的是父類(lèi)的屬性 ? ?父類(lèi)沒(méi)有則調(diào)用的是子類(lèi)的屬性;調(diào)用方法時(shí)也是 父類(lèi)子類(lèi)都有而且一樣的 調(diào)用的是父類(lèi) ?子類(lèi)重寫(xiě)了方法調(diào)用的是子類(lèi)的方法;對(duì)于構(gòu)造方法 在new時(shí)候 先調(diào)用父類(lèi)構(gòu)造方法 ?不管子類(lèi)的構(gòu)造方法是否被super(這個(gè)用super調(diào)用父類(lèi)構(gòu)造器再加子類(lèi)新屬性或者是不加而定義的新構(gòu)造器不叫重寫(xiě)也不叫重載 就是一個(gè)書(shū)寫(xiě)子類(lèi)的通識(shí)規(guī)范);但是還是先調(diào)用父類(lèi)構(gòu)造器(???我要是不用super呢?) 再調(diào)用子類(lèi)的構(gòu)造器 所以父類(lèi)構(gòu)造器如果有語(yǔ)句在用new的時(shí)候一定會(huì)被執(zhí)行
錯(cuò)題整理1_crystalsugar_的博客-CSDN博客_在animal類(lèi)和其子類(lèi)dog類(lèi)中均定義了一個(gè)名為age的整形變量,則按照多態(tài)原理,下列程
1-6?使用上轉(zhuǎn)型對(duì)象調(diào)用子類(lèi)重寫(xiě)的方法時(shí)表現(xiàn)出多態(tài)性。
T
Java中什么是上轉(zhuǎn)型對(duì)象, 上轉(zhuǎn)型對(duì)象如何體現(xiàn)多態(tài)?_大圖書(shū)館的牧羊人的博客-CSDN博客_上轉(zhuǎn)型對(duì)象
對(duì)象的上轉(zhuǎn)型對(duì)象和多態(tài)_I_N_A的博客-CSDN博客_上轉(zhuǎn)型和多態(tài)
1-7?可以使用throws語(yǔ)句來(lái)指明方法有異常拋出。
T
throw 是語(yǔ)句拋出一個(gè)異常;throws 是方法拋出一個(gè)異常_Hustudent20080101的博客-CSDN博客_可以使用throws語(yǔ)句來(lái)指明方法有異常拋出
1-8?對(duì)于泛型類(lèi)或泛型接口,在編譯過(guò)程中檢查泛型類(lèi)型的合法性,在對(duì)象進(jìn)入和離開(kāi)方法的邊界處添加類(lèi)型檢查和類(lèi)型轉(zhuǎn)換的機(jī)制,并且泛型信息會(huì)保持到運(yùn)行時(shí)階段。
F
二、單選題補(bǔ)充
2-1 HashSet集合存儲(chǔ)下列哪種元素時(shí),要確保不出現(xiàn)重復(fù)的元素,必須重寫(xiě)hashcode方法和equals方法()
D.自定義的Student類(lèi)
2-2?作者?劉鳳良?單位?天津仁愛(ài)學(xué)院
對(duì)于break語(yǔ)句和continue語(yǔ)句,說(shuō)法正確的是( )。
B.continue語(yǔ)句只應(yīng)用于循環(huán)體中
2-3?作者?殷偉鳳?單位?浙江傳媒學(xué)院
Math.floor(3.6)的結(jié)果是( )。
C.3.0
floor向下取最大整數(shù)
2-4 作者?呂行軍?單位?河北農(nóng)業(yè)大學(xué)
Java中,Arrays類(lèi)屬于哪個(gè)包
C.java.util
2-5?作者?周雪芹?單位?山東理工大學(xué)
下面程序的輸出結(jié)果為:( )。
class A {
double f(double x, double y) {
return x * y;
}
}
class B extends A {
double f(double x, double y) {
return x + y;
}
}
public class Test {
public static void main(String args[]) {
A obj = new B();
System.out.println(obj.f(4, 6));
}
}
A.10.0
2-6 作者?殷偉鳳?單位?浙江傳媒學(xué)院
可以用于在子類(lèi)中調(diào)用被重寫(xiě)父類(lèi)方法的關(guān)鍵字是()
D.super
2-7?作者?劉永福?單位?河北農(nóng)業(yè)大學(xué)
在Java中,以下()類(lèi)的對(duì)象是以鍵-值的方式存儲(chǔ)對(duì)象。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-422237.html
C.HashMap文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-422237.html
到了這里,關(guān)于2021級(jí)Java程序設(shè)計(jì)課程練習(xí)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!