JAVA语言程序设计课后习题----第四单元解析(仅供参考)
1 本題水題,主要理解題目的意思即可,訪問方法和修改方法可以通過快捷方式alt+insert選中你需要的成員變量即可
1 public class Person { 2 public String name; 3 public int age; 4 public static void main(String[] args) { 5 // new一個對象,對象名是person 6 Person person =new Person(); 7 // 給name變量賦值 8 person.setName("zzy"); 9 // 給age變量賦值 10 person.setAge(21); 11 // 對象的引用 12 person.speak(); 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 public void speak() { 32 System.out.println(getName()); 33 System.out.println(getAge()); 34 } 35 }2 本題水題,跟著題目的意思寫出需求即可,注意的是在無參的構(gòu)造函數(shù)調(diào)用有參的構(gòu)造函數(shù)的方法,本題的centerX與centerY沒有用到
1 public class Circle { 2 public double centerX,centerY,radius; 3 public static void main(String[] args) { 4 System.out.println("調(diào)用帶參數(shù)的構(gòu)造函數(shù)圓的各種數(shù)據(jù):"); 5 Circle circle=new Circle(1.0); 6 System.out.println("圓的半徑:"+circle.getRadius()); 7 System.out.println("圓的面積:"+circle.getRrea()); 8 System.out.println("圓的周長:"+circle.getPerimeter()); 9 System.out.println("調(diào)用不帶參數(shù)的構(gòu)造函數(shù)圓的各種數(shù)據(jù):"); 10 Circle circle1=new Circle(); 11 System.out.println("圓的半徑:"+circle1.getRadius()); 12 System.out.println("圓的面積:"+circle1.getRrea()); 13 System.out.println("圓的周長:"+circle1.getPerimeter()); 14 15 16 } 17 //訪問 18 public double getRadius() { 19 return radius; 20 } 21 22 public void setRadius(double radius) { 23 this.radius = radius; 24 } 25 public double getRrea(){ 26 return (Math.PI*radius*radius); 27 } 28 public double getPerimeter(){ 29 return 2*Math.PI; 30 } 31 32 public Circle(double radius) { 33 this.radius = radius; 34 } 35 public Circle() { 36 // 注意在無參的構(gòu)造函數(shù)中調(diào)用有參的構(gòu)造函數(shù)要用this 37 // 具體在書本71詳解 38 this(1.0); 39 } 40 }3 本題水題,注意在無參的構(gòu)造函數(shù)調(diào)用有參的構(gòu)造函數(shù)的方法
1 public class Rectangle { 2 public double length,width; 3 4 public static void main(String[] args) { 5 Rectangle rectangle =new Rectangle(1.0,1.0); 6 System.out.println("定義帶參數(shù)的構(gòu)造函數(shù)矩形:"); 7 System.out.println("長方形的長為:"+rectangle.getLength()); 8 System.out.println("長方形的寬為:"+rectangle.getWidth()); 9 System.out.println("長方形的面積為:"+rectangle.getArea()); 10 System.out.println("長方形的周長為:"+rectangle.getPerimeter()); 11 System.out.println("定義不帶參數(shù)的構(gòu)造函數(shù)矩形"); 12 Rectangle rectangle1 =new Rectangle(); 13 14 System.out.println("長方形的長為:"+rectangle1.getLength()); 15 System.out.println("長方形的寬為:"+rectangle1.getWidth()); 16 System.out.println("長方形的面積為:"+rectangle1.getArea()); 17 System.out.println("長方形的周長為:"+rectangle1.getPerimeter()); 18 19 } 20 // 有參的構(gòu)造方法 21 public Rectangle(double length, double width) { 22 this.length = length; 23 this.width = width; 24 } 25 // 無參的構(gòu)造方法 26 public Rectangle(){ 27 this(1.0,1.0); 28 } 29 30 public double getLength() { 31 return length; 32 } 33 34 public void setLength(double length) { 35 this.length = length; 36 } 37 38 public double getWidth() { 39 return width; 40 } 41 42 public void setWidth(double width) { 43 this.width = width; 44 } 45 // 矩形的周長 46 public double getPerimeter(){ 47 return 2*(length+width); 48 } 49 // 矩形的面積 50 public double getArea(){ 51 return length*width; 52 } 53 }4 本題水題,主要是加深對構(gòu)造方法的使用
1 public class Triangle { 2 public double a,b,c,s; 3 4 public static void main(String[] args) { 5 Triangle triangle = new Triangle(3,4,5); 6 System.out.println("調(diào)用帶三個參數(shù)的構(gòu)造函數(shù):"); 7 System.out.println("三角形的面積為:"+triangle.area()); 8 System.out.println("調(diào)用默認構(gòu)造函數(shù):"); 9 Triangle triangle1 =new Triangle(); 10 System.out.println("三角形的面積為:"+triangle1.area()); 11 12 } 13 // 有參的構(gòu)造方法 14 public Triangle(double a, double b, double c) { 15 this.a = a; 16 this.b = b; 17 this.c = c; 18 s=(a+b+c)/2; 19 } 20 // 無參的構(gòu)造方法 21 public Triangle() { 22 this.a = 0.0; 23 this.b = 0.0; 24 this.c = 0.0; 25 s=(a+b+c)/2; 26 } 27 // 三角形的面積 28 public double area(){ 29 return Math.sqrt(s*(s-a)*(s-b)*(s-c)); 30 } 31 }5 本題主要是加深對訪問方法與修改方法的使用
1 public class Stock { 2 public String symbol,name; 3 public double perviousPrice,currentPrice; 4 5 public static void main(String[] args) { 6 Stock stock =new Stock("600000","浦發(fā)銀行"); 7 stock.setPerviousPrice(25.5); 8 stock.setCurrentPrice(28.6); 9 System.out.print("代號為:"+stock.symbol+"的"+stock.name); 10 System.out.print("當前的市值變化百分比為:"+String.format("%.1f",stock.getChangPercent())); 11 12 } 13 // 返回從當前一日價格到當前價格變換的百分比 14 public double getChangPercent(){ 15 return getCurrentPrice()-getPerviousPrice(); 16 } 17 // 有參的構(gòu)造方法 18 public Stock(String symbol, String name) { 19 this.symbol = symbol; 20 this.name = name; 21 } 22 // 返回儲存股票的前一日收盤價 23 public double getPerviousPrice() { 24 return perviousPrice; 25 } 26 // 設(shè)置儲存股票的前一日收盤價 27 public void setPerviousPrice(double perviousPrice) { 28 this.perviousPrice = perviousPrice; 29 } 30 // 返回存儲股票的當前價格 31 public double getCurrentPrice() { 32 return currentPrice; 33 } 34 // 設(shè)置存儲股票的當前價格 35 public void setCurrentPrice(double currentPrice) { 36 this.currentPrice = currentPrice; 37 } 38 }6 Fibonacci數(shù)就是第一項第二項都是1從第三項開始是前兩項之和,可以通過調(diào)用方法來實現(xiàn),里面用if else語句
1 public class Fibonacci { 2 // 調(diào)用方法 3 public static long fib(int n){ 4 // 如果n==1或者2就返回1 5 if(n==1||n==2) 6 return 1; 7 else 8 return fib(n-2)+fib(n-1); 9 } 10 public static void main(String[] args) { 11 for (int i = 1; i <= 20; i++) { 12 System.out.print(fib(i)+" "); 13 // 每10項進行一次換行 14 if (i % 10 ==0) 15 System.out.println(); 16 } 17 18 } 19 }7 本題主要考的是方程的判別式問題 與方程的根求解問題,記住這些基本公式問題就不大
1 public class QuadraticEquation { 2 private double a,b,c; 3 public static void main(String[] args) { 4 QuadraticEquation quadraticEquation =new QuadraticEquation(1,3,1); 5 if (quadraticEquation.getDiscriminant()<0) 6 System.out.println("方程無根"); 7 if (quadraticEquation.getDiscriminant()==0) 8 System.out.println(quadraticEquation.getRoot1()); 9 if (quadraticEquation.getDiscriminant()>0) 10 System.out.println("x1="+quadraticEquation.getRoot1()+" "+"x2="+quadraticEquation.getRoot2()); 11 12 } 13 // 帶有參數(shù)的構(gòu)造方法 14 public QuadraticEquation(double a, double b, double c) { 15 this.a = a; 16 this.b = b; 17 this.c = c; 18 } 19 20 public double getA() { 21 return a; 22 } 23 24 public double getB() { 25 return b; 26 } 27 28 public double getC() { 29 return c; 30 } 31 // 判別式 32 public double getDiscriminant(){ 33 return b*b-4*a*c; 34 } 35 // 方程一個根 36 public double getRoot1(){ 37 return (-b+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a; 38 } 39 // 方程第二個根 40 public double getRoot2(){ 41 return (-b-+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a; 42 } 43 }8 本題就是對成員變量的基本運用注意題目意思即可
1 public class TV { 2 private int channel,volumeLevel; 3 private boolean on; 4 5 public static void main(String[] args) { 6 } 7 // 無參構(gòu)造函數(shù) 8 public TV(){ 9 } 10 // 電視開 11 public void turnOn(){ 12 on = true; 13 } 14 // 電視關(guān) 15 public void turnOff(){ 16 on = false; 17 } 18 // 設(shè)置頻道 19 public void setChannel(int channel) { 20 this.channel = channel; 21 } 22 // 設(shè)置音量 23 public void setVolumeLevel(int volumeLevel) { 24 this.volumeLevel = volumeLevel; 25 } 26 // 頻道+開關(guān) 27 public void channelUp(){ 28 channel++; 29 } 30 // 頻道-開關(guān) 31 public void channelDown(){ 32 channel--; 33 } 34 // 聲音+開關(guān) 35 public void volumeUp(){ 36 volumeLevel++; 37 } 38 // 聲音-開關(guān) 39 public void volumeDown(){ 40 volumeLevel--; 41 } 42 }9 本題主要是對基本概念的理解,比如偶數(shù)、奇數(shù)的判斷
1 public class MyInteger { 2 private int value; 3 4 public static void main(String[] args) { 5 6 MyInteger myInteger =new MyInteger(10); 7 System.out.println("value:"); 8 System.out.println("偶數(shù):"+myInteger.isEven()); 9 System.out.println("奇數(shù):"+myInteger.isOdd()); 10 System.out.println("素數(shù):"+myInteger.isPrime()); 11 System.out.println("返回參數(shù)15:"); 12 System.out.println("偶數(shù):"+myInteger.isEven(15)); 13 System.out.println("奇數(shù):"+myInteger.isOdd(15)); 14 System.out.println("素數(shù):"+myInteger.isPrime(15)); 15 // char []a={'a','b','c'}; 16 // myInteger.parseInt(a); 17 char A[]={'1','2','3'}; 18 //myInteger.parseInt(A); 19 System.out.println(myInteger.parseInt(A)); 20 String s = "123"; 21 System.out.println(myInteger.parseInt(s)); 22 } 23 24 public MyInteger(int value) { 25 this.value = value; 26 } 27 28 public int getValue() { 29 return value; 30 } 31 // 判斷value是否為偶數(shù) 32 public boolean isEven(){ 33 return value%2==0; 34 } 35 // 判斷value是否為奇數(shù) 36 public boolean isOdd(){ 37 return value%2!=0; 38 } 39 // 判斷返回參數(shù)整數(shù)是否為素數(shù) 40 public boolean isPrime(){ 41 int i; 42 for(i=2;i<value;i++) { 43 if (value % i == 0) 44 break; 45 } 46 if (value==i) 47 return true; 48 return false; 49 } 50 // 判斷返回參數(shù)整數(shù)是否為偶數(shù) 51 public boolean isEven(int number){ 52 return number%2==0; 53 54 } 55 // 判斷返回參數(shù)整數(shù)是否為奇數(shù) 56 public boolean isOdd(int number){ 57 return number%2!=0; 58 59 } 60 // 判斷返回參數(shù)整數(shù)是否為素數(shù) 61 public boolean isPrime(int number){ 62 int i; 63 for(i=2;i<number;i++) { 64 if (number % i == 0) 65 break; 66 } 67 if (number==i) 68 return true; 69 return false; 70 71 } 72 // 判斷返回參數(shù)整數(shù)對象是否為偶數(shù) 73 public boolean isEven(MyInteger myInteger){ 74 return myInteger.value % 2 ==0; 75 76 } 77 // 判斷返回參數(shù)整數(shù)對象是否為奇數(shù) 78 public boolean isOdd(MyInteger myInteger){ 79 return myInteger.value % 2 != 0; 80 } 81 // 判斷返回參數(shù)整數(shù)對象是否為素數(shù) 82 public boolean isPrime(MyInteger myInteger){ 83 84 int i; 85 for(i=2;i<myInteger.value;i++) { 86 if (myInteger.value % i == 0) 87 break; 88 } 89 if (myInteger.value==i) 90 return true; 91 return false; 92 } 93 // 比較當前對象整數(shù)與參數(shù)整數(shù) 94 public boolean equals(int b){ 95 return b==value; 96 97 } 98 // 比較當前對象整數(shù)與參數(shù)整數(shù)對象 99 public boolean equals(MyInteger myInteger){ 100 return myInteger.value==value; 101 102 } 103 // 將參數(shù)字符數(shù)組轉(zhuǎn)換為整數(shù) 104 public int parseInt(char []a){ 105 String s=new String(a); 106 return parseInt(s); 107 } 108 // 將參數(shù)字符串轉(zhuǎn)換成整數(shù) 109 public int parseInt(String s){ 110 return Integer.valueOf(s); 111 } 112 113 }10 本題主要考的是回文數(shù)與素數(shù),我們首先判斷這個數(shù)是否為素數(shù),在這個基礎(chǔ)上再判斷是否為回文數(shù),素數(shù)就是只能被1和本身整除的數(shù),回文數(shù)就是一個數(shù)第一位與最后一位相等,第二位與倒數(shù)第二位相等,比如11、101
1 public class HuiSu { 2 public int i,j,k,count=0; 3 4 public static void main(String[] args) { 5 HuiSu huiSu =new HuiSu(); 6 huiSu.print(); 7 } 8 public void print(){ 9 for ( j = 2; j < 1000 ; j++) { 10 for ( k = 2; k <1000 ; k++) { 11 if (j%k==0) 12 break; 13 } 14 // 判斷是是否是素數(shù) 15 if (j == k) 16 { 17 // 判斷是否為回文數(shù) 18 if (j / 10 == 0) 19 { 20 count++; 21 System.out.print(j+" "); 22 if (count % 10==0) 23 System.out.println(); 24 } 25 if (j / 10==j%10) 26 { 27 count++; 28 System.out.print(j+" "); 29 if (count % 10==0) 30 System.out.println(); 31 } 32 if(j / 100==j % 10) 33 { 34 count++; 35 System.out.print(j+" "); 36 if (count % 10 == 0 && count != 20) 37 System.out.println(); 38 } 39 } 40 } 41 } 42 }?
10 本題雖然是最后一題,但是仍然是水題,就是對成員變量的運用
1 import java.time.LocalDate; 2 3 public class Account { 4 private int id; 5 private double balance,annulRate; 6 private LocalDate dateCreated; 7 8 public static void main(String[] args) { 9 Account account = new Account(123,0.0); 10 System.out.println("尊敬的:"+account.id+"用戶"+"恭喜你開通賬戶"); 11 System.out.println("你的賬戶余額為:"+account.balance); 12 //創(chuàng)建賬戶的時間 13 account.getDateCreated(); 14 //存款 15 account.deposit(500); 16 //取款 17 account.withdraw(10); 18 System.out.println("你要辦理的業(yè)務(wù):"); 19 account.setAnnulRate(3.6); 20 account.getMonthlyInterestRate(); 21 22 } 23 // 有參構(gòu)造函數(shù) 24 public Account(int id, double balance) { 25 this.id = id; 26 this.balance = balance; 27 } 28 // 無參構(gòu)造函數(shù) 29 public Account() { 30 } 31 32 public int getId() { 33 return id; 34 } 35 // 返回賬戶余額 36 public double getBalance() { 37 return balance; 38 } 39 // 返回存款的年利率 40 public double getAnnulRate() { 41 return annulRate; 42 } 43 //獲取當前日期 44 public LocalDate getDateCreated() { 45 // LocalDate dateCreated= LocalDate.now(); 46 LocalDate today = LocalDate.now(); 47 today.getYear(); 48 System.out.println("您賬戶創(chuàng)建的日期為:"+LocalDate.now()); 49 // System.out.println(today.getYear()+"/"+today.getMonthValue()+"/"+today.getDayOfMonth()+"/"); 50 return dateCreated; 51 } 52 // 返回月利率的方法 53 public double getMonthlyInterestRate(){ 54 System.out.println("您存款的月利率為:"+annulRate/12); 55 return annulRate/12; 56 57 } 58 //取款方法 59 public void withdraw(double amount){ 60 balance -= amount; 61 System.out.println("取款金額為:"+amount); 62 System.out.println("當前余額為:"+balance); 63 64 } 65 // 存款方法 66 public void deposit(double amount){ 67 balance += amount; 68 System.out.println("存款金額為:"+amount); 69 System.out.println("當前余額為:"+balance); 70 } 71 // 設(shè)置賬戶ID 72 public void setId(int id) { 73 this.id = id; 74 } 75 // 設(shè)置賬戶的余額 76 public void setBalance(double balance) { 77 this.balance = balance; 78 } 79 // 修改存款的年利率 80 public void setAnnulRate(double annulRate) { 81 System.out.println("您的存款年利率為:"+annulRate); 82 this.annulRate = annulRate; 83 } 84 }?
轉(zhuǎn)載于:https://www.cnblogs.com/PerZhu/p/10865271.html
總結(jié)
以上是生活随笔為你收集整理的JAVA语言程序设计课后习题----第四单元解析(仅供参考)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【学习】SpringBoot之自定义拦截
- 下一篇: 吴裕雄 python 神经网络——Ten