天津商業(yè)大學(xué)JAVA復(fù)習(xí)資料_第1頁(yè)
天津商業(yè)大學(xué)JAVA復(fù)習(xí)資料_第2頁(yè)
天津商業(yè)大學(xué)JAVA復(fù)習(xí)資料_第3頁(yè)
天津商業(yè)大學(xué)JAVA復(fù)習(xí)資料_第4頁(yè)
天津商業(yè)大學(xué)JAVA復(fù)習(xí)資料_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、明天幾號(hào)public class Date2 int year, month, day;public void setdate(int y, int m, int d) year = y;month = m;day = d;void today() System.out.println("The date of today is " + year + "/" + month + "/"+ day);boolean isleap(int y) return (y % 4 = 0 && y % 100 != 0) | ye

2、ar % 400 = 0;void tomorrow() int d, m, y;d = day + 1;m = month;y = year;if (d > 28) && month = 2) if (!isleap(year) | d > 29) d = 1;m = m + 1; else if (d > 30&& (month < 7 && month % 2 = 0 | month > 7 && month % 2 = 1) d = 1;m = m + 1; else if (d > 3

3、1) d = 1;m = m + 1; if (m = 13) y = y + 1;m = 1; System.out.println("The date of tomorrow is " + y + "/" + m + "/" + d);public static void main(String args) Date2 de = new Date2();de.setdate(2004, 2, 28);if (de.isleap(de.year)System.out.println(de.year + " is a lea

4、p year.");de.today();de.tomorrow();兔子對(duì)數(shù)package Sy;public class Rabbit public static void main(String args) int month = 4;Rabbit r = new Rabbit();System.out.print(month + "月共有" + r.getNumber(month)+"對(duì)兔子。");public int getNumber(int month) int num1 = 1, num2 = 1;int num3 = 0;if

5、 (month = 1)return num1;else if (month = 2)return num2;else for (int i = 3; i <= month; i+) num3 = num1 + num2;num1 = num2;num2 = num3;return num3;輸出1131135311357531135797531package Ch3;/類似楊輝三角public class YH public static void main(String args) int i;int yh = new int5;for (i = 0; i < 5; i+) y

6、hi = new int2 * i + 1;/ 第i行有(2*i+1)個(gè)元素yhi0 = 1;yhi2 * i = 1;for (i = 1; i < 5; i+)/ i:1-4;j:1-7(0,8上的數(shù)為0)for (int j = 1; j < 2 * i; j+)yhij = yhi - 1j - 1 + 2;/ 前一行前一列加2for (i = 0; i < 5; i+) for (int j = 0; j <= 2 * i; j+)System.out.print(yhij + "t");System.out.println();或publ

7、ic class YH public static void main(String args) int i,j,k; for(i=0;i<=4;i+) for ( j=0;j<20-i;j+) System.out.print(" "); for (k=0;k<=2*i;k+) if (k<=i) System.out.print(" "+(2*k+1); else System.out.print(" "+(2*(2*i-k)+1); System.out.println(); 鍵盤(pán)輸入m,輸出小于等于m的

8、所有素?cái)?shù)for (i = 2; i <= m; i+) p = true;for (int j = 2; j < i; j+)if (i % j = 0) p = false;break;if (p = true) System.out.print(i + "t"); n+; if (n % 10 = 0) System.out.println();輸出一整數(shù)的所有因子 for(int i=1;i<=m;i+) if (m%i=0) System.out.print(i+"t");多少天后鋼材小于5米 int d=0;float m=2

9、000; while (m>=5) m=m/2; d+; System.out.print(d+": "); System.out.println(m); System.out.print("You need "+d+" days"); 月份英文名輸出 int m; String str; String month="January","February","March","April","May","June"

10、;,"July","August","september","October","November","December" BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in); System.out.print("Input the m:"); str=buf.readLine(); m=Integer.parseInt(str); if (m>=1

11、&& m<=12) System.out.println(monthm-1); else System.out.print("Your Input is wrong"); 或者可以用P55 switch 語(yǔ)句單詞計(jì)數(shù)int n = 0;for (int i = 0; i < s1.length(); i+) char m = s1.charAt(i);if(Character.toLowerCase(m)<97| Character.toLowerCase(m) > 122)&& Character.toLowerCa

12、se(s1.charAt(i - 1) >= 97&& Character.toLowerCase(s1.charAt(i - 1) <= 122)n = n + 1;System.out.println(n);0-9出現(xiàn)次數(shù)int m;int a=new int10;long aa=1586488654895649l;for(int i=0;i<=15;i+)m=(int) (aa%10);am=am+1;aa=aa/10; for(m=0;m<10;m+)System.out.println(m+" :"+am);包的引用pack

13、age Ch4;import java.util.*;public class Date private int year,month,day;public Date(int y,int m,int d)year=y;month=m;day=d;public Date();public int thisyear() /方法this year 得到當(dāng)前年份return Calendar.getInstance().get(Calendar.YEAR);package Ch4;import Ch4.Date; /導(dǎo)入Ch4包中的Date類class Person String name;int a

14、ge;public Person(String na, int ag) name = na;age = ag;public Person() public int birth(int y) /得到出生年份return y - age + 1;public class Person_ex public static void main(String args) Person ps = new Person("Tom", 21);Date now = new Date();int y = now.thisyear();System.out.println( + &

15、quot; was born in " + ps.birth(y);第四章1import java.util.*;public class Person private String name;private char sex;private int year, month;public Person() public Person(String nm, char sx, int y, int m) name = nm;sex = sx;year = y;month = m;public void printPerson() Calendar now = Calendar.getIn

16、stance();int age = now.get(Calendar.YEAR) - year;System.out.println("Name: " + name + ",Sex: " + sex + ", Age: " + age);public static void main(String args) Person pe1 = new Person("Tom", 'm', 1980, 10);pe1.printPerson();飲料味道public abstract class Drink

17、 final static int coffee=1;final static int beer=2;final static int milk=3;public abstract void taste();public static Drink getDrink(int drinkType)switch(drinkType)case 1:return new Coffee();case 2:return new Beer();case 3:return new Milk();default:return null;public class Coffee extends Drinkpublic

18、 void taste()System.out.println("咖啡: 苦");package Sy;import java.io.*;public class Test public static void main(String args) throws IOExceptionBufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in);System.out.println("請(qǐng)輸入飲料類型(1-3):");String str=""trys

19、tr=buf.readLine();catch(Exception e)int drinkType=Integer.parseInt(str);Drink drink=Drink.getDrink(drinkType);if(drink=null)System.out.println("對(duì)不起!沒(méi)有您輸入的飲料類型。");elsedrink.taste();多種構(gòu)造方法,不同的構(gòu)造方法創(chuàng)建對(duì)象(求面積)public class Rectangle double width, length, girth, area;public Rectangle() ;public Rec

20、tangle(double wd, double le) width = wd;length = le;public void setWidth(double wd) width = wd;public void setLength(double le) length = le;public double getWidth() return width;public double getLength() return length;public double girth() return 2 * (width + length);public double area() return widt

21、h * length;public void printRectangle() System.out.println("Width=" + width + ", Length=" + length);public static void main(String args) Rectangle re1 = new Rectangle(10, 20);Rectangle re2 = new Rectangle();re2.setWidth(3);re2.setLength(4);re1.printRectangle();System.out.println(

22、"Girth=" + re1.girth() + ", Area=" + re1.area();re2.printRectangle();System.out.println("Girth=" + re2.girth() + ", Area=" + re2.area();子串的查找public class SubString public static void main(String args) String str1 = new String("addActionListener");Str

23、ing str2 = new String("Action");int n;n = str1.indexOf(str2);if (n >= 0) System.out.println("String_2 is in String_1");System.out.println("The substring before String_2 is "+ str1.substring(0, n);System.out.println("The substring behind String_2 is "+ str1.

24、substring(n + str2.length();elseSystem.out.println("String_2 is not in String_1");學(xué)生管理系統(tǒng)public class Student String stuno, name;float math, english, computer;public Student() public Student(String stuno, String name, float math, float english,float computer) this.stuno = stuno; =

25、name;this.math = math;this.english = english;puter = computer;public String getStuno() return stuno;public void setStuno(String stuno) this.stuno = stuno; 。public void setComputer(float computer) puter = computer;public float sum() float s;s = this.math + this.english + puter;return s;public float a

26、vg() float avg;avg = this.sum() / 3;return avg;public float max() /最大值float max = this.math;if (this.english > max)max = this.english;if (puter > max)max = puter;return max;public float min() /最小值float min = this.math;if (this.english < min)min = this.english;if (puter < min)min = puter;

27、return min;package Sy;public class StudentOperation Student stuArray;int num = 0;public StudentOperation() stuArray = new Student30;public Student getStuArray() return stuArray;public int getNum() return num;public boolean addStudent(Student student) if (num < 30) stuArraynum = student;num+;retur

28、n true; else System.out.println("stuArray.lenth" + stuArray.length);System.out.println("數(shù)組越界,不能增加。");return false;public boolean delStudent(String stuno) for (int i = 0; i < stuArray.length; i+)if (stuArrayi.getStuno().equals(stuno) for (int j = i; j < num; j+)stuArrayj = s

29、tuArrayj + 1;stuArray29 = null;return true;return false;public Student queryStuno(String stuno) for (int i = 0; i < num; i+)if (stuArrayi.getStuno().equals(stuno) System.out.println("學(xué)號(hào)" + "t" + "姓名" + "t" + "數(shù)學(xué)" + "t"+ "英語(yǔ)" +

30、"t" + "計(jì)算機(jī)");System.out.println(stuArrayi.getStuno() + "t"+ stuArrayi.getName() + "t" + stuArrayi.getMath()+ "t" + stuArrayi.getEnglish() + "t"+ stuArrayi.getComputer();return stuArrayi;return null;public Student queryName(String name) for

31、(int i = 0; i < num; i+)if (stuArrayi.getName().equals(name) return stuArrayi;return null;public boolean updStu(Student stuno) for (int i = 0; i < num; i+)if (stuArrayi.getStuno().equals(stuno.getStuno() stuArrayi = stuno;return true;return false;public void print() System.out.println("學(xué)號(hào)

32、" + "t" + "姓名" + "t" + "數(shù)學(xué)" + "t" + "英語(yǔ)"+ "t" + "計(jì)算機(jī)");tryfor (int i = 0; i < num; i+)System.out.println(stuArrayi.getStuno() + "t"+ stuArrayi.getName() + "t" + stuArrayi.getMath()+ "t&qu

33、ot; + stuArrayi.getEnglish() + "t"+ stuArrayi.getComputer() + "t");catch(NullPointerException e)public void statistics() System.out.println("學(xué)科" + "t" + "平均成績(jī)" + "t" + "最高分" + "t" + "最低分"+ "t");System

34、.out.print("數(shù)學(xué)" + "t");float avg = 0, sum = 0;for (int i = 0; i < num; i+)sum += stuArrayi.getMath();avg = sum / num;System.out.print(avg + "t");float max = 0;for (int i = 0; i < num; i+)if (stuArrayi.getMath() > max)max = stuArrayi.getMath();System.out.print(m

35、ax + "t");float min = 100;for (int i = 0; i < num; i+)if (stuArrayi.getMath() < min)min = stuArrayi.getMath();System.out.println(min + "t");System.out.print("英語(yǔ)" + "t");float avg1 = 0, sum1 = 0;for (int i = 0; i < num; i+)sum1 += stuArrayi.getEnglish(

36、);avg1 = sum1 / num;System.out.print(avg1 + "t");float max1 = 0;for (int i = 0; i < num; i+)if (stuArrayi.getEnglish() > max1)max1 = stuArrayi.getEnglish();System.out.print(max1 + "t");float min1 = 100;for (int i = 0; i < num; i+)if (stuArrayi.getEnglish() < min1)min1

37、 = stuArrayi.getEnglish();System.out.println(min1 + "t");System.out.print("計(jì)算機(jī)" + "t");float avg2 = 0, sum2 = 0;for (int i = 0; i < num; i+)sum2 += stuArrayi.getComputer();avg2 = sum2 / num;System.out.print(avg2 + "t");float max2 = 0;for (int i = 0; i <

38、num; i+)if (stuArrayi.getComputer() > max2)max2 = stuArrayi.getComputer();System.out.print(max2 + "t");float min2 = 100;for (int i = 0; i < num; i+)if (stuArrayi.getComputer() < min2)min2 = stuArrayi.getComputer();System.out.println(min2 + "t");package Sy;import java.io.

39、*;public class StudentManagement public static void main(String args) StudentManagement sm = new StudentManagement();StudentOperation so = new StudentOperation();int menu;while (true) menu = sm.printMenu();if (menu = 1) Student s = sm.addStudent();so.addStudent(s);if (menu = 2) String stuno = sm.que

40、ry();so.delStudent(stuno);if (menu = 3) String stuno = sm.query();so.queryStuno(stuno);if (menu = 4) Student student = sm.updStudent();so.updStu(student);if (menu = 5) so.print();if (menu = 6) so.statistics();if (menu = 7) break;public Student addStudent() Student student = new Student();System.out.

41、println("請(qǐng)輸入學(xué)號(hào): ");BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);String str = ""try str = buf.readLine(); catch (Exception e) student.setStuno(str);System.out.println("請(qǐng)輸入姓名: ");buf = new BufferedReader(new InputStreamReader(System.in);str

42、 = ""try str = buf.readLine(); catch (Exception e) student.setName(str);System.out.println("請(qǐng)輸入數(shù)學(xué): ");buf = new BufferedReader(new InputStreamReader(System.in);str = ""try str = buf.readLine(); catch (Exception e) float math = Float.parseFloat(str);student.setMath(math)

43、;System.out.println("請(qǐng)輸入英語(yǔ): ");buf = new BufferedReader(new InputStreamReader(System.in);str = ""try str = buf.readLine(); catch (Exception e) float english = Float.parseFloat(str);student.setEnglish(english);System.out.println("請(qǐng)輸入計(jì)算機(jī): ");buf = new BufferedReader(new I

44、nputStreamReader(System.in);str = ""try str = buf.readLine(); catch (Exception e) float computer = Float.parseFloat(str);student.setComputer(computer);return student;public String query() System.out.println("請(qǐng)輸入學(xué)號(hào): ");BufferedReader buf;buf = new BufferedReader(new InputStreamRea

45、der(System.in);String str = ""try str = buf.readLine(); catch (Exception e) return str;public Student updStudent() Student student = new Student();System.out.println("請(qǐng)輸入學(xué)號(hào): ");BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);String str = ""try str = buf.readLine(

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論