天天看點

第十四章

1.根據目錄結構myjava\practice1\Foo.java.寫出foo類的包名。

包名:package myjava.practice1;

2.改寫第12章簡答題第3題中的電腦類(Calculator)。要求将加減乘除的方法改寫帶參數的方法,再定義一個運算方法ope(),接收使用者選擇的運算和兩個數字,根據使用者選擇的運算計算結果。

public class Calculator {

int s;//運算結果

public int ope(int op,int num1,int num2){

switch(op){

case 1:

s=num1+num2;

break;

case 2:

s=num1-num2;

break;

case 3:

s=num1*num2;

break;

case 4:

s=num1/num2;

break;

default:

System.out.println(“請輸入正确的數字:”);

break;

}

return s;

}

}

public class TesCalc {

public static void main(String[] args) {

Calculator a=new Calculator();

Scanner input=new Scanner(System.in);

System.out.println(“請輸入第一個數:”);

int num1=input.nextInt();

System.out.println(“請輸入第二個數:”);

int num2=input.nextInt();

System.out.println(“請選擇運算:1.加法2.減法3.乘法4.除法”);

int choose=input.nextInt();

System.out.println(“運算結果為:”+a.ope(choose,num1,num2));

}
           

}

第三題:模拟一個簡單的購房商貸月供電腦,假設按照以下的公式計算出總利息和每月還款金額,

總利息 = 貸款金額 × 利率

每月還款金額 = (貸款金額 + 總利息) ÷ 貸款年限

貸款年限不同,利率是不相同的,規定隻有三總年限,利率

年限:3年(36個月) ,利率:6.03%

年限:5年(60個月) ,利率:6.12%

年限:20年(240個月) ,利率:6.39%

代碼如下:

public class Loan {

int month; //定義月限

double rate; //定義利率

double amount;//定義每月還款金額

double money; //定義總利息

public double loan(double loan,int yearchoice){

switch(yearchoice){

case 1:

rate=0.0603;

money=loanrate;

month=36;

break;

case 2:

rate=0.0612;

money=loanrate;

month=60;

break;

case 3:

rate=0.0639;

money=loan*rate;

month=240;

break;

default:

System.out.println(“請輸入正确年份:”);

break;}

amount=(loan+money)/month;

System.out.println("*****月供為:"+ amount);

return amount;

}
}
           

public class LoanTest {

public static void main(String[] args) {

Loan a=new Loan();

Scanner input=new Scanner(System.in);

System.out.println(“請輸入貸款金額:”);

int loan=input.nextInt();

System.out.println(“請選擇貸款年限:1.3年(36個月) 2.5年(60個月) 3.20年(240個月)”);

int se=input.nextInt();

a.loan(loan, se);

}

}

第四題:根據指定不同的行及字元,生成不同的三角形。

代碼如下:

public class Xing {

public void printTriangle(int row,String ch){

for (int i = 1; i <=row; i++) {

for (int j = 1; j <=i; j++) {

System.out.print(ch);

}
		System.out.println("");
	}
}
           

}

public class XingTest {

public static void main(String[] args) {

Xing a=new Xing();

Scanner input=new Scanner(System.in);

System.out.print(“請輸入行高:”);

int row1=input.nextInt();

System.out.print(“請輸入列印的字元:”);

String ch1=input.next();

a.printTriangle(row1,ch1);

}

}

第五題:根據三角形的三條邊長,判斷其是直角,鈍角,還是銳角三角形,程式要求如下:

先輸入三角形三條邊的邊長

判斷是否構成三角形,構成三角形的條件是”任意兩邊之和大于第三邊”,如果不能構成三角形則提示”不是三角形”

如果能構成三角形,判斷三角形是何種三角形,如果三角形有一條邊的平方等于其他兩條邊平方的和,則為直角三角形,如果有一條邊的平方大于其他兩條邊平方的和,則為鈍角三角形,否則為,直角三角形。

第十四章
第十四章

第六題:編寫向整型數組的指定位置插入元素,并輸出插入前後數組的值 。

public class shuzu {

public void insertArray(int[] arr, int index, int value) {

int array[] = new int[arr.length + 1];

for (int i = 0; i < arr.length; i++) {

array[i] = arr[i];

}

for (int i = array.length - 1; i > index; i–) {

array[i] = array[i - 1];

}

array[index] = value;

System.out.println(“插入後的數組:”);

for (int i = 0; i < array.length; i++) {

if (array[i] != 0) {

System.out.print(array[i] + “\t”);

}

}

}

}

public class shuzuTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

shuzu sc = new shuzu();

int arr[] = new int[7];

arr[0] = 1;

arr[1] = 2;

arr[2] = 3;

arr[3] = 4;

arr[4] = 5;

arr[5] = 6;

arr[6] = 7;

System.out.println(“插入前的數組:”);

for (int i = 0; i < arr.length; i++) {

if (arr[i] != 0) {

System.out.print(arr[i] + “\t”);

}

}

System.out.print("\n請輸入要插入數組的位置:");

int index = input.nextInt();

System.out.print(“請輸入要插入的整數:”);

int value = input.nextInt();

sc.insertArray(arr, index, value);

}

}

第七題:本學期期末學員共參加了三門課的考試,即Java,C# , SQL,編寫方法計算每位同學三門課程的平均分

代碼如下:public class Student {

int java; //Java成績

int C; //C#成績

int sql; //資料庫成績

Student[] student = new Student[30];
    public void getAvg(Student stu) {
        for (int i = 0; i < student.length; i++) {
            if (student[i] == null) {
                student[i] = stu;
                break;
            }
        }
    }
    public void getavg(int arr[], int num) {
        // 參數分别為:java成績,c#成績,資料庫成績,學生人數
        double[] arr2 = new double[num + 1];
        for (int i = 0; i < num + 1; i++) {
            arr2[i] = arr[i] / 3.0;
        }
        for (int i = 0; i < arr2.length; i++) {
            System.out.println("第" + (i + 1) + "位同學的平均分為:" + arr2[i]);
        }
    }

}
           

public class StudentTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

Student stu[] = new Student[30]; // 建立學生類對象數組

Student stus = new Student(); // 建立Student類對象

Student student = new Student(); // 建立學生類對象

int js[] = new int[stu.length]; // 建立一個數組來接收每一個學生的總成績

int index = 0; // 擷取學生的人數

for (int i = 0; i < stu.length; i++) {

int tatleScore = 0; // 初始化總成績并每次清零;

System.out.println(“第” + (i + 1) + “位同學的成績為:”);

System.out.print(“java成績為:”);

student.java = input.nextInt();

System.out.print(“c#的成績為:”);

student.C = input.nextInt();

System.out.print(“SQL的成績為:”);

student.sql = input.nextInt();

tatleScore = student.java + student.C + student.sql;

for (int j = 0; j < js.length; j++) { // 循環用數組接收總成績

if (js[i] == 0) {

js[i] = tatleScore;

break;

}

}

System.out.print(“是否繼續輸入(y/n):”);

String flag = input.next();

System.out.println("");

stus.getAvg(student); // 調用方法增加學生資訊

if (flag.equals("n")) {
            index = i;
            System.out.println("輸入完成!");
            break;
        }
    }
    System.out.println("學生的平均分為:");
    stus.getavg(js, index); // 調用方法
}
           

}