




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、6. 定義一個類Initial,類的內部重載initArr()方法,分別實現對整型數組、雙精度型數組、字符串數組的初始化功能,數組作為方法的參數,方法體內提示用戶為數組元素輸入數據,然后顯示所有元素。在mian方法中創建三種類型的數組,創建Initial類的對象,分別調用initArr()方法進行初始化。import java.util.*;public class OverLoad2 public static void main(String args) / TODO Auto-generated method stubScanner reader = new Scanner(System
2、.in);Initial init = new Initial();int a = new int6;init.initArr(a);double b = new double5;init.initArr(b);String s = new String3;init.initArr(s);class InitialScanner reader = new Scanner(System.in);void initArr(int arr) System.out.println(初始化整型數組); for(int i=0;iarr.length;i+) System.out.println(請輸入第
3、+(i+1)+個整數); arri = reader.nextInt(); System.out.println(整型數組初始化完畢,所有元素為:); for(int i=0;iarr.length;i+) System.out.print(arri+ ); System.out.println(n-); void initArr(double arr) System.out.println(初始化雙精度型數組); for(int i=0;iarr.length;i+) System.out.println(請輸入第+(i+1)+個雙精度數); arri = reader.nextDouble
4、(); System.out.println(雙精度型數組初始化完畢,所有元素為:); for(int i=0;iarr.length;i+) System.out.print(arri+ ); System.out.println(n-); void initArr(String arr) System.out.println(初始化字符串型數組); for(int i=0;iarr.length;i+) System.out.println(請輸入第+(i+1)+個字符串); arri = reader.next(); System.out.println(字符串型數組初始化完畢,所有元素
5、為:); for(int i=0;i=0.5)return n+1;elsereturn n;static long round(double a)long n = (long)a;if(a-n=0.5)return n+1;elsereturn n;生命周期及作用域1. 定義一個按身高計算標準體重的類StdWeight,其中包含兩個靜態的成員方法:forMale(double h)計算男子標準體重、forFemale(double h)計算女子標準體重,兩個方法均帶返回值。在應用程序類的main方法中提示用戶輸入身高和性別,調用StdWeight類的方法得到標準體重,顯示結果。計算公式:標準
6、體重(男)=(身高cm-100)x0.9(kg) 標準體重(女)=(身高cm-100)x0.9(kg)-2.5(kg) 正常體重:標準體重-(多少)10提示:控制實數保留N位小數:(如保留2位小數)import java.text.DecimalFormat;double num = 21.3749;DecimalFormat df = new DecimalFormat(0.00);String s = df.format(num); /21.37參考代碼:import java.util.*;import java.text.*;public class Demo public stati
7、c void main(String args) Scanner reader = new Scanner(System.in);System.out.println(請輸入您的身高(單位厘米);double height = reader.nextDouble();System.out.println(請選擇您的性別:1.男 2.女);int sex = reader.nextInt();DecimalFormat df = new DecimalFormat(0.00); if(sex=1)System.out.println(標準體重是+df.format(StdWeight.forFe
8、male(height)+公斤);else if(sex=2)System.out.println(標準體重是+df.format(StdWeight.forMale(height)+公斤);elseSystem.out.println(您輸入的選擇不正確。);class StdWeightstatic double forMale(double h)return (h-100)*0.9;static double forFemale(double h)return (h-100)*0.9-2.5; Java常用類與函數庫1. 提示用戶輸入三角型兩邊邊長與其夾角度數,利用公式s=1/2absi
9、n(c)計算三角型面積,輸出結果。注意:正弦值的計算對象是弧度制的角,需將角度轉換為弧度:/180。import java.util.Scanner;import java.text.DecimalFormat;class Demo public static void main(String args)Scanner reader = new Scanner(System.in);System.out.println(請輸入三角型兩邊邊長);double a = reader.nextDouble();double b = reader.nextDouble();System.out.pri
10、ntln(請輸入兩邊夾角度數);double angle = reader.nextDouble();double s = 0.5*a*b*Math.sin(angle*Math.PI/180);DecimalFormat df = new DecimalFormat(0.00);String area = df.format(s);System.out.println(該三角型面積是+area);/System.out.println(Math.sin(90);2. 編寫Java應用程序,使用Vector向量來保存用戶輸入的若干字符串。循環讀入用戶輸入的字符串,以end作為結束。將所有字符串
11、顯示出來。在所有字符串的中間位置插入“NICE”,再次顯示所有字符串。import java.util.Vector;import java.util.Scanner;class Demo public static void main(String args)Scanner reader = new Scanner(System.in);Vector vct = new Vector(1,1);System.out.println(請輸入字符串,以輸入end作為結束);String str;dostr = reader.next();vct.add(str);while(!str.equal
12、s(end);System.out.println(您剛才輸入的所有字符串是:);System.out.println(vct.toString();System.out.println(插入NICE到中間位置:);int n = vct.capacity();vct.insertElementAt(NICE,n/2);System.out.println(vct.toString();3. 顯示InputDialog輸入對話框實現對用戶輸入的英文單詞進行簡單處理(轉換為大寫、轉換為小寫、反轉顯示)。程序運行效果如下圖:import javax.swing.JOptionPane;class
13、Demo public static void main(String args)String str = JOptionPane.showInputDialog(請輸入一個英文單詞);str = str.trim();String items = 轉換為大寫, 轉換為小寫, 反轉顯示 ; Object selectedValue = JOptionPane.showInputDialog(null, 請選擇, 輸入, JOptionPane.INFORMATION_MESSAGE, null, items, items0);String choice = (String)selectedVa
14、lue;if(choice.equals(轉換為大寫)JOptionPane.showMessageDialog(null,str.toUpperCase(),操作結果,JOptionPane.INFORMATION_MESSAGE);else if(choice.equals(轉換為小寫)JOptionPane.showMessageDialog(null,str.toLowerCase(),操作結果,JOptionPane.INFORMATION_MESSAGE);else JOptionPane.showMessageDialog(null,(new StringBuffer(str).
15、reverse(),操作結果,JOptionPane.INFORMATION_MESSAGE);對象的進一步探討-面向對象中的繼承1. 定義一個球類Ball,包含一個私有成員變量半徑(double r),兩個公有成員方法:設定半徑值方法( void setR(double x) 、 得到半徑值方法(double getR( ))。定義一個臺球類Billiards,繼承Ball類,包含一個私有成員變量顏色(String color),兩個公有成員方法:設定顏色方法(void setCol (String clo))、輸出信息方法(void show()),其中show方法可以輸出該臺球的顏色和半
16、徑值。定義一個公有類,測試前兩個類。class Ballprivate double r;public void setR(double x)r = x;public double getR()return r;class Billiards extends Ballprivate String color;public void setCol(String col)color = col;public void show()System.out.println(臺球的顏色是+color+,半徑是+getR();public class Demopublic static void main(
17、String args)Ball b1 = new Ball();b1.setR(10);System.out.println(球的半徑是+b1.getR();Billiards b2 = new Billiards();b2.setR(5);b2.setCol(藍色);b2.show();2. 定義材料類Material,包含:保護的成員變量名稱、單價(String name; double price;)為數據初始化賦值的構造方法;公有的成員方法得到所有信息(public String toString())。定義木材類Wood,繼承自材料類。包含:私有的成員變量顏色(String col
18、;)為數據初始化賦值的構造方法;覆蓋公有的成員方法得到所有信息(public String toString())。定義公共類,測試上述兩個類的使用。思考:程序中把木材類中toString()方法的public 去掉會產生什么結果?為什么?程序中把材料類中toString()方法的public 去掉會產生什么結果?為什么?class Materialprotected String name;protected double price;Material(String n,double p)name = n;price = p;public String toString()return na
19、me+的價格是+price;class Wood extends Materialprivate String col;Wood(String n,double p,String c)super(n,p);col = c;public String toString()return name+的價格是+price+,顏色是+col;public class Demopublic static void main(String args)Material m = new Material(大理石,1000);System.out.println(m.toString();Wood w = new
20、 Wood(檀木,1200,紅棕);System.out.println(w.toString();思考題:木材類中toString()方法的public 去掉程序會出錯,因為該方法由材料類繼承而來,子類覆蓋父類的方法時不能縮小其訪問權限,public去掉就會變成包訪問權限,此處不能去掉。 材料類中toString()方法的public 去掉會產生同樣的問題,因為toString方法不是材料類自定義的,是所有類的父類Object類提供的。3. 改寫例7-8,給Shape類增加構造方法,能夠對顏色進行初始化設置,思考兩個子類的構造方法該如何修改才能使程序正常運行。class Demo publi
21、c static void main(String args)Circle c = new Circle(blue,1);c.printInfo();Rectangle rect = new Rectangle(green,3,4);rect.printInfo();class ShapeString color;Shape(String c)color = c;double getArea()return 0;class Circle extends Shapedouble radius;Circle(String c, double r)super(c);radius = r;double
22、 getArea()return Math.PI*radius*radius;void printInfo()System.out.println(圓的顏色是:+color+面積是+getArea();class Rectangle extends Shapedouble width, height;Rectangle(String c, double w, double h)super(c);width = w;height = h;double getArea()return width*height;void printInfo()System.out.println(矩形的顏色是:+c
23、olor+面積是+getArea();深入多態1. 要使程序運行后出現如下結果,該如何修改程序代碼。去掉Person類前面的final,去掉Student類中show()方法前面的final。class Person String name;char sex;Person()Person(String n,char s)name = n;sex = s;void show()System.out.println(name is +name+, sex is +sex);class Student extends Personint number;Student()Student(String
24、n, char s, int num)name = n;sex = s;number = num;void show()System.out.println(name is +name+, sex is +sex+,number is +number);class Pupil extends Studentdouble hcScore;Pupil()Pupil (String n, char s, int num,double hcs)name = n;sex = s;number = num;hcScore = hcs;void show()System.out.println(name i
25、s +name+, sex is +sex+,number is +number+,Score is +hcScore);public class App01public static void main(String s)Person p = new Person(小明, 男);p.show();Student stu = new Student(小明, 男,101);stu.show();Pupil pu = new Pupil(小明, 男,101,95);pu.show();2. 求正方形的面積a)創建一個接口IShape ,接口中有一個抽象方法public double area( );b)定義一個類square,且實現IShape接口。square類有一個屬性表示正方形的邊長;構造方法初始化該邊長。c)定義一個主類,在此類中,創建square類的實例,求該正方形面積。interface IShapepublic double area();class square implements IShapedouble length ;public square(double l)length = l;public do
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《高中校園文化發展》課件
- 《會計實務手工操作》課件
- 《招聘的策略》課件
- 鐵路調車工作實訓無線調車燈顯設備課件
- 鐵路工程安全技術石家莊鐵路44課件
- 鐵路貨物運雜費保價費率相關規定課件
- 《GB 15562.1-1995環境保護圖形標志 排放口(源)》(2025版)深度解析
- 中世紀文化課件
- 股東資金借用合同范例
- 東陽木雕文化課件
- 大學生心理健康與發展學習通超星期末考試答案章節答案2024年
- 《平行四邊形》全章復習教學設計
- (新版)高級考評員職業技能鑒定考試題庫(含答案)
- 藥劑科考試試題
- 2024年中國醫養及康復醫療產業發展研究報告
- 《人工智能基礎》課件-AI的前世今生:她從哪里來
- 扎實推動科技創新和產業創新深度融合
- 聲紋鑒定知識考核試題
- 養殖業勞動合同樣本
- 保險公司增額終身壽主講課件
- 上海市2023-2024學年五年級下冊第1-3單元期中模擬測試數學試卷(滬教版)
評論
0/150
提交評論