5-1Java继承中子父类中重名成员变量的访问
super.num
5-2Java多态、普通类继承抽象类实现接口
public Animal(){}
public abstract void eat();
public abstract void sleep();
public Cat(){
super();}
public JumpCat(){
super();
}
5-3ava继承、抽象类、接口的使用
public Animal(){}; public abstract void eat(); public abstract void sleep();
public void jump(); public JumpCat(){}
5-4Java中String作为实参方法调用
change(s);
5-5ava中StringBuffer对象作为实参的方法调用
change(sb);
5-6Diverse Array
int i=0;i<arr.length;i++ sum=sum+arr[i]; arr2D.length int[] row:arr2D
sums[rowNum]=arraySum(row);
5-7一维数组
int i=0;i<arr.length;i++ sum+=arr[i];
5-8最大单词长度
in.next(); line.split(" "); String s:a;
5-9Java中,集合嵌套之ArrayList嵌套ArrayList
ArrayList<ArrayList<Person> al = new ArrayList<>();
al.add(classOne);
al.add(classTow);
System.out.println(p,toString());
6-1 数字翻转
public static int rNum(int num){
int result=0;
while(num>0) {
result=result*10+num%10;
num/=10;
}
return result;
}
6-2快递计价器
abstract class Express {
protected int weight;
public Express(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
public abstract int getTotal();
}
class SLExpress extends Express {
public SLExpress(int weight) {
super(weight);
}
public int getTotal() {
int total = 12;
if (weight > 1) {
total += (weight - 1) * 2;
}
return total;
}
}
class DDExpress extends Express {
public DDExpress(int weight) {
super(weight);
}
public int getTotal() {
int total = 5;
if (weight > 1) {
total += (weight - 1) * 1;
}
return total;
}
}
class CaicaiStation {
public static int calculate(Express[] ex) {
int total = 0;
for (Express e : ex) {
total += e.getTotal();
}
return total;
}
}
6-3网络报文
class Message{
String proto;
String psd;
public Message(String proto,String psd){
this.proto=proto;
this.psd=psd;
}
public boolean equals(Message a2){
if(this.proto.equals(a2.proto)&&this.psd.equals(a2.psd)){
return true;
}
else {
return false;
}
}
public String toString(){
String str=new String();
str="<"+this.proto+">"+this.psd;
return str;
}
}
6-4 处理IllegalArgumentException异常
class NewLoan{
double AnnuallnterestRate; //定义变量年利率
int NumOfYears; //定义变量年数
double LoanAmount; //定义变量贷款总额
public NewLoan(double AnnuallnterestRate, int NumOfYears, double LoanAmount) throws IllegalArgumentException{
//初始化年利率、年数、贷款总额
this.AnnuallnterestRate = AnnuallnterestRate;
this.NumOfYears = NumOfYears;
this.LoanAmount = AnnuallnterestRate;
if(AnnuallnterestRate <= 0){ //若年利率<=0,抛出异常及相应描述
throw new IllegalArgumentException("Annual interest rate must be positive.");
}
if(NumOfYears <= 0){ //若年数<=0,抛出异常及相应描述
throw new IllegalArgumentException("Number of years must be positive.");
}
if(LoanAmount <= 0){ //若贷款总额<=0,抛出异常及相应描述
throw new IllegalArgumentException("Loan amount must be positive.");
}
}
//构建方法,计算月利率,返回月返还额
public double getMonthlyPayment(){
double monthlyInterestRate = AnnuallnterestRate / 1200; // 计算月利率
return LoanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), NumOfYears * 12))); // 返回每月还款额
}
//构建方法,计算年若年利率
public double getTotalPayment(){
return getMonthlyPayment() * NumOfYears * 12000 - 0.001; // 返回总还款额
}
//构造printTotalPayment()方法,返回总额,结果保留3位小数
public String printTotalPayment(){
return "totalPayment is " + String.format("%.3f", getTotalPayment()); // 格式化
}
}
6-5 停车场收费系统-21-jc-ST
class Bus extends Vehicle{
private int seats;
public Bus(){
;
}
public Bus(float price,int minute,int seats){
super(price,minute);
this.seats=seats;
}
public int getSeats(){
return seats;
}
public void setSeats(int seats){
this.seats=seats;
}
public float computeFee(){
int h=getMinute()/60;
int f=getMinute()%60;
float p = getPrice();
float sum=h*p;
if(f>0&&f<=30){
return sum+(float)0.5*p;
}
else if(f>30){
return sum+p;
}
return sum;
}
}
class Car extends Vehicle{
public Car(){
}
public Car(float price,int minute){
super(price,minute);
}
public float computeFee(){
int h=getMinute()/60;
int f=getMinute()%60;
float p = getPrice();
float sum=h*p;
if(f>30){
return sum+p;
}
else{
return sum;
}
}
}
7-1 Point3D类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x=input.nextInt();
int y=input.nextInt();
int z=input.nextInt();
Point3D p3d=new Point3D(x,y,z);
System.out.println("Point3D("+p3d.getX()+","+p3d.getY()+","+p3d.getZ()+")");
}
}
class Point {
private int x;
private int y;
public Point(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
class Point3D extends Point{
private int z;
public Point3D(int x,int y,int z){
super(x,y);
this.z=z;
}
public int getZ(){
return z;
}
}
7-2 矩阵转置运算
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N=input.nextInt();
int [][] arr=new int[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
arr[i][j]=input.nextInt();
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
System.out.print(arr[j][i]);
if(j!=N-1){
System.out.print(" ");
}
}
System.out.println();
}
}
}
7-3商品检索系统
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int N=input.nextInt();
int []A=new int[n+1];
for(int i=1;i<=n;i++){
A[i]=input.nextInt();
}
A[0]=0;
int []f=new int[N];
f[0]=0;
for(int i=0;i<=n-1;i++){
for(int j=A[i];j<A[i+1];j++){
f[j]=i;
}
}
for(int i=A[n];i<N;i++){
f[i]=n;
}
int sum=0;
for(int i=0;i<N;i++){
sum+=f[i];
}
System.out.println(sum);
}
}
8-1 根据问题回答
将问题进行分解有以下好处:
1. 简化复杂问题:将大型系统或任务分解成更小、更可管理的子任务,使问题变得更易于理解和解决。
2. 组织和协调:通过将任务按照结构和顺序进行分解,可以更好地组织和协调团队成员的工作。
3. 明确任务范围:分解任务可以明确各个子任务的范围和目标,使团队成员对所需工作的理解更加清晰。
4. 精确估算工作量:通过分解任务,可以更准确地估算每个子任务的工作量,从而更好地进行时间和资源管理。
5. 支持进度控制:任务分解结构可以用来跟踪任务的进度,有助于发现延迟和问题,并及时采取措施解决。
6. 促进沟通和协作:通过将任务分解为更小的部分,可以促进团队成员之间的沟通和协作,提高项目整体效率。
7. 提高透明度和可视化:任务分解结果可以通过图表或层级结构进行展示,使整个项目的结构和进度更加透明和可视化。
10-1 统计每个学生的选课门数和考试总成绩,并按选课门数升序排列
SELECT
····sc.sno·AS·学号,
····COUNT(sc.cno)·AS·选课门数,
····SUM(sc.score)·AS·考试总成绩
FROM
····sc
GROUP·BY·sc.sno
ORDER·BY·选课门数·ASC;