1、Java复习编程题30课案第一章:1编写类College,College中有一个方法init(),显示“威海职业学院”,编写类School,调用init()方法,在屏幕上显示3行“威海职业学院”。public class Collegepublic void init()System.out.println(威海职业学院);public class Schoolpublic static void main(String a)College c1=new College ();for (int i=0;i3;i+)c1.init ();第二章:1、自定义类Days及其方法dayInmonth(
2、 )。该方法的功能是返回用户输入月份的天数(二月份就按28天计算)。例如用户输入3,则该方法将返回值31;用户输入4,则该方法将返回值30。通过main()方法将返回值显示出来。class Daysstaticint dayInmonth(int a)int s=31;switch(a)case 2: s=28;break;case 4: case 6: case 9: case 11: s=30;return s;public static void main(String args)int a=Integer.parseInt(args0);int b=dayInmonth(a);Syst
3、em.out.println(a+月有+b+天);第三章:1编写程序计算12+22+32+42+972+982+992+1002的值,输出class Qiuhestatic int sum=0;public static int cal()for (int i=1;i=100;i+)sum+=i*i;return sum;public static void main(String args)System.out.println(计算结果为+cal ();2.打印输出10行杨晖三角形class yanghuipublic static void main (String args)int i,
4、j;int yhlevel=10;int yanghui;System.out.println(杨晖三角形:);yanghui=new intyhlevel;for(i=0;iyanghui.length;i+)yanghuii=new inti+1;yanghui00=1;for (i=1; iyanghui.length;i+)yanghuii0=1;for(j=1;jyanghuii.length-1;j+)yanghuiij=yanghuii-1j-1+yanghuii-1j;yanghuiiyanghuii.length-1=1;for (i=0; iyanghui.length;i
5、+)for(j=0;jyanghuii.length;j+)System.out.print(yanghuiij+ );System.out.println(); 输出结果是:杨晖三角形:1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1第四章:1.自定义一数组并对数组中每个元素赋值,然后按逆序输出.2.编写一个程序用选择法对数组a=20,10,50,40,30,70,60,80,90
6、,100进行由大到小的排序。importjava.io.*; publicclassArrayTest publicstaticvoidmain(Stringargs) inti; inta=newint5; for(i=0;i=0;i-) System.out.println(a+i+=ai); 2.import java.io.*; public class SelectSortpublic static void main(String args)int a=20,10,50,40,30,70,60,80,90,100;int temp;for (int i=0; ia.length-1
7、;i+)for (int j=i+1; ja.length ; j+)if (aiaj) temp=ai;ai=aj; aj=temp; for (int k=0;ka.length ;k+)System.out.println(a+k+:+ak);第五章:1设计一个长方形类,成员变量包括长和宽。类中有计算面积和周长的方法,并有相应的set方法和get方法设置和获得长和宽。编写测试类测试是否达到预定功能。要求使用自定义的包。2设计雇员Employee类,记录雇员的情况,包括姓名、年薪、受雇时间,要求定义MyDate类作为受雇时间,其中包括工作的年、月、日,并用相应的方法对Employee类进行
8、设置。编写测试类测试Employee类。要求使用自己的包。3 根据下面的要求编程实现复数类ComplexNumber。(1)复数类ComplexNumber的属性m_dRealPart实部,代表复数的实数部分。m_dlmaginPart虚部,代表复数的虚数部分。(2)复数类ComplexNumber的方法ComplexNumber()构造方法,将实部、虚部都置为0。ComplexNumber(doubler,doublei)构造方法,创建复数对象的同时完成复数的实部、虚部的初始化;r为实部的初值,i为虚部的初值。getRealPart()获得复数对象的实部。getlmaginaryPart()
9、获得复数对象的虚部。setRealPart(doubled)把当前复数对象的实部设置为给定的形参的数字。setlmaginaryPart(doubled)把当前复数对象的虚部设置为给定形参的数字。complexAdd(ComplexNumberc)当前复数对象与形参复数对象相加,所得的结果也是复数值,返回给此方法的调用者。complexAdd(doublec)当前复数对象与形参实数对象相加,所得的结果仍是复数值,返回给此方法的调用者。complexMinus(ComplexNumberc)当前复数对象与形参复数对象相减,所得的结果也是复数值,返回给此方法的调用者。1 。package mypa
10、ckage.math;class Rectangleprivate int length;private int width;public Rectangle(int length,int width)this.length=length;this.width=width;public void set(int length,int width)this.length=length;this.width=width;public void get()System.out.println(此长方形的长为+length+宽为+width);public int getLength()return
11、length;public int getWidth()return width;public int calculatePerimeter()return 2*(length+width);public int calculateArea()return (length*width);public void print()System.out.println(此长方形的周长为+calculatePerimeter()+面积为+calculateArea();class Testpublic static void main(String args)Rectangle myrectangle=
12、new Rectangle(15,10);myrectangle.get();myrectangle.print();myrectangle.set(20,15);System.out.println(长方形的长是+myrectangle.getLength()+宽是+ myrectangle .getWidth();myrectangle.print();2. package mypackage.employee;class MyDateprivate int year;private int month;private int day;public MyDate(int year,int
13、month,int day)this.year=year;this.month=month;this.day=day;public int getYear()return year; public class Employeeprivate String name;private double salary;private MyDate hireDay;public Employee(String name,double salary,MyDate day)this.name=name;this.salary=salary;hireDay=day;public void print()Syst
14、em.out.println(name+ +salary+hireYear();public int hireYear()return hireDay.getYear();class TestEmployeepublic static void main(String args)Employee a=new Employee(李明,20000,new MyDate(2000,10,1);Employee b=new Employee(王强,24000,new MyDate(1995,8,12);a.print();b.print();3public class ch5_e5_12public
15、static void main(String args)ComplexNumber cNumber_1 = new ComplexNumber(3,-5);ComplexNumber cNumber_2 = new ComplexNumber(2,2);double d = 10.0;System.out.println(cNumber_1.toString() + 加 + cNumber_2.toString() + 等于 + cNumber_plexAdd(cNumber_2).toString();System.out.println(cNumber_1.toString() + 加
16、+ d + 等于 + cNumber_plexAdd(d).toString();System.out.println(cNumber_1.toString() + 减 + cNumber_2.toString() + 等于 + cNumber_plexMinus(cNumber_2).toString();System.out.println(cNumber_1.toString() + 减 + d + 等于 + cNumber_plexMinus(d).toString();System.out.println(cNumber_1.toString() + 乘 + cNumber_2.to
17、String() + 等于 + cNumber_plexMulti(cNumber_2).toString();System.out.println(cNumber_1.toString() + 乘 + d + 等于 + cNumber_plexMulti(d).toString();class ComplexNumber/域private double m_dRealPart;private double m_dImaginPart;/构造方法ComplexNumber()m_dRealPart = 0.0;m_dImaginPart = 0.0;ComplexNumber(double r
18、,double i)m_dRealPart = r;m_dImaginPart = i;ComplexNumber(ComplexNumber c)m_dRealPart = c.getRealPart();m_dImaginPart = c.getImaginaryPart();/get,set方法double getRealPart()return m_dRealPart;double getImaginaryPart()return m_dImaginPart;void setRealPart(double d)m_dRealPart = d;void setImaginaryPart(
19、double d)m_dImaginPart = d;/复数运算方法ComplexNumber complexAdd(ComplexNumber c)return new ComplexNumber(this.m_dRealPart + c.getRealPart(),this.m_dImaginPart + c.getImaginaryPart();ComplexNumber complexAdd(double c)return new ComplexNumber(this.m_dRealPart + c, this.m_dImaginPart);ComplexNumber complexM
20、inus(ComplexNumber c)return new ComplexNumber(this.m_dRealPart - c.getRealPart(),this.m_dImaginPart - c.getImaginaryPart();ComplexNumber complexMinus(double c)return new ComplexNumber(this.m_dRealPart - c, this.m_dImaginPart);ComplexNumber complexMulti(ComplexNumber c)return new ComplexNumber(this.m
21、_dRealPart * c.getRealPart() - this.m_dImaginPart * c.getImaginaryPart(),this.m_dRealPart * c.getImaginaryPart()+ this.m_dImaginPart * c.getRealPart();ComplexNumber complexMulti(double c)return new ComplexNumber(this.m_dRealPart * c, this.m_dImaginPart * c);/toString()public String toString()return
22、( + m_dRealPart + + + m_dImaginPart + i + );第六章:1有三个字符串,编写程序找出其中最大者。2编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。3设定5个字符串,要求只打印那些以字母“b”开头的串,编写程序完成。4设定一个有大小写字母的字符串和一个查找字符,使用类String方法IndexOf()来判断在该字符串中该字符出现的次数。1public class max1public static void main(String args)String s1= Hello Java;String s2=
23、Java Applet;String s3= Java;String s;if(pareTo(s2)0)s=s2;else s=s1;if(pareTo(s3)0)s=s3;Systrm.out.println(big=+s);2public class outpublic static void main(String args)String s1= Hello Java;StringBuffer bufup=new StringBuffer();StringBuffer buflow=new StringBuffer();for(int I=0;Is1.length();I+)if(s1.
24、charAt(i)97)bufup.append(s1.charAt(i);bufup.append( );else buflow.append(s1.charAt(i);buflow.append( );System.out.println(s1= +s1);System.out.println( uppercase= +bufup);System.out.println( lowercase= +buflow);3public class prin_b public static void main(String args)String s1=hello;String s2=bee;Str
25、ing s3=java;String s4=brove;String s5=bag;if(s1.indexOf(b)= =0)System.out.println(s1);if(s2.indexOf(b)= =0)System.out.println(s2);if(s3.indexOf(b)= =0)System.out.println(s3);if(s4.indexOf(b)= =0)System.out.println(s4);if(s5.indexOf(b)= =0)System.out.println(s5);4public class ppppublic static void ma
26、in(String args)String s1=abcdefghi;int num=0;for (int i=0;is1.lastIndexOf(e);i+)i=s1.indexof(e,i);num+;System.out.print( s1=+s1+e=);if(num= =0)System.out.println( no found);else System.out.println(num);第七章:1、编一个应用程序,按行顺序地读取一个可读文件的内容2、使用对象的串行化机制,使用RandomAccessFile构造一个应用程序能够随机存取文件的记录,可显示、修改记录的内容3编写一个程
27、序,从键盘输入一串字符,统计这串字符中英文字母、数字、其他符号的字符数。4编写一个程序,从键盘输入一串字符,从屏幕输出并将其存入a.txt文件中。5编写一个程序,从键盘输入10个整数,并将这些数据排序后在标准输出上输出。编程第1题import java.io.*;public class ReadFile public static void main(String args) String s;FileInputStream is;InputStreamReader ir;BufferedReader in;tryis=new FileInputStream(bbs.txt); ir=new InputStreamReader(is); in=new BufferedReader(ir); while(s=in.readLine()!=null) System.out.println(Read: +s); catch(FileNotFoundException e)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1