《编程练习题(共11页).doc》由会员分享,可在线阅读,更多相关《编程练习题(共11页).doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上编程练习题1 给出一百分制成绩,要求输出成绩等级A,B,C,D,E。90分以上的为A,8089分为B,7079分为C,6069为D,60分以下为E。(1)源程序:public class TestScore public static void main(String args)int score;int sc= 95,83,70,62,50;for(int i=0;i=4;i+)score=sci;switch(score/10)case 10:;case 9: System.out.println(score +score+ is A); break;case 8
2、: System.out.println(score +score+ is B); break;case 7: System.out.println(score +score+ is C); break;case 6: System.out.println(score +score+ is D); break;default: System.out.println(score +score+ is E); break;(2) 运行结果:2 编程实现对数组元素由大到小排列。(1)源程序:public class ArraySort public static void main(String a
3、rgs) int as=12,2,45,19,78,15,23,-10,9,-40;for(int i=1;i=9;i+)for(int j=0;j=9-i;j+)int t;if(asjasj+1)t=asj;asj=asj+1;asj+1=t;for(int i=0;i=9;i+)System.out.print( +asi+ );(2)运行结果:3 打印出所有的“水仙花数”。所谓“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身。例如:153是一个“水仙花数”,因为153=13+53+33。(1)源程序:public class FlowerNumber public stati
4、c void main(String args) for(int i=100;i0)System.out.print(num.real+num.imag+i);else if(num.imag 0)System.out.print(num.real+num.imag+i);elseSystem.out.print(num.real);public void add(Complex num1,Complex num2)Complex num3=new Complex(0,0);num3.imag=num1.imag+num2.imag;num3.real=num1.real+num2.real;
5、System.out.print();outcomplex(num1);System.out.print()+();outcomplex(num2);System.out.print()=);outcomplex(num3);System.out.println();public void less(Complex num1,Complex num2)Complex num3=new Complex(0,0);num3.imag=num1.imag-num2.imag;num3.real=num1.real-num2.real;System.out.print();outcomplex(num
6、1);System.out.print()-();outcomplex(num2);System.out.print()=);outcomplex(num3);System.out.println();public void mult(Complex num1,Complex num2)Complex num3=new Complex(0,0);num3.imag=num1.imag*num2.real+num1.real*num2.imag;num3.real=num1.real*num2.real-num1.imag*num2.imag;System.out.print();outcomp
7、lex(num1);System.out.print()*();outcomplex(num2);System.out.print()=);outcomplex(num3);System.out.println();public class ComplexOperation public static void main(String args) / TODO Auto-generated method stubComplex com1=new Complex(2,-4);System.out.print(The first complex is:);com1.outcomplex(com1)
8、;System.out.println();Complex com2=new Complex(15,4);System.out.print(The second complex is:);com1.outcomplex(com2);System.out.println();com1.add(com1,com2);com1.less(com1,com2);com1.mult(com1,com2);(2)运行结果:5编程创建一个Box类,在其中定义三个变量表示一个立方体的长、宽和高,定义一个构造方法对这三个变量进行初始化,然后定义一个方法求立方体的体积。创建一个对象,求给定尺寸的立方体的体积。(1
9、)源程序:class Boxprivate float length,width,heigth;public Box(float l,float w,float h)this.length=l;this.width=w;this.heigth=h;public float boxVolume()return this.length*this.width*this.heigth;public class BoxTest public static void main(String args) / TODO Auto-generated method stubBox box1=new Box(4,
10、5,6);System.out.println(This boxs volume is:+box1.boxVolume();(2)运行结果:6定义一个学生类(Student),属性包括:学号,班号,姓名,性别,年龄,班级总人数;方法包括:获得学号,获得班号,获得姓名,获得性别,获得年龄,获得班级总人数,修改学号,修改班号,修改姓名,修改性别,修改年龄以及一个toString()方法将Student类中的所有属性组合成一个字符串。定义一个学生数组对象。设计程序进行测试。(1)源程序:class Studentprivate String name,number,classnum,sex;priv
11、ate int age;private static int allnum=0;public Student(String na,String num,String cla,String se,int ag)name=na;number=num;classnum=cla;sex=se;age=ag;allnum+;public String getName()return name;public String getNumber()return number;public String getClassNumber()return classnum;public String getSex()
12、return sex;public int getAge()return age;public int getAllNumber()return allnum;public String toString()return name+number+classnum+sex+age+allnum;public class StudentTest public static void main(String args) Student stu=new Student5; stu0=new Student(周,boy,21);stu1=new Student(扈,boy,21);stu2=new St
13、udent(张,boy,19);stu3=new Student(李,girl,19);stu4=new Student(杨,girl,23); for(int i=0;i5;i+) System.out.println(stui.getName();System.out.println(stui.toString(); (2)运行结果:7.定义个类MyRectangle代表矩形,为矩形定义getLength方法(获得矩形的长度)、getWidth方法(获得矩形的宽度)、setLength方法(设置矩形的长度)、setWidth方法(设置矩形的宽度)、getArea方法(求矩形的面积)和toS
14、tring方法(显示矩形的格式),为矩形派生出一个子类MySquare代表正方形,并对getArea和toString进行重写。并编写程序进行测试。(1)源程序:class MyRectangleprivate float length,width;public MyRectangle()public void setLength(float length)this.length=length;public void setWidth(float width)this.width=width;public float getLength()return length;public float
15、getWidth()return width;public float getArea()return length*width;public String toString()String str;str=This is a Rectangle.Length is +length+ Width is +width;return str;class MySquare extends MyRectangleprivate float side;public MySquare()public void setSide(float side)this.side=side;public float g
16、etSide()return side;public float getArea()return side*side;public String toString()String str;str=This is a Square.Side is +side;return str;public class ReloadTest public static void main(String args) MyRectangle rec=new MyRectangle();rec.setLength(10);rec.setWidth(5);System.out.println(rec.toString
17、();System.out.println(Area is +rec.getArea();MySquare squ=new MySquare();squ.setSide(10);System.out.println(squ.toString();System.out.println(Area is +squ.getArea();(2)运行结果:8编写一个类实现地址的概念,包括的属性有“国家”、“省份”、“市县”、“街道”、“门牌”、“单位”、“邮编”,自行定义方法封装这些属性,并定义一个方法按照标准格式打印出寄给该地址的信封。并从该地址类派生出国内、国际两种地址,两种地址的格式不同;重载打印信
18、封的方法,新方法不再在方法内部直接执行打印操作,而是返回一个按格式组合好的地址字符串。(1)源程序:class Addressprivate String country,province,city,street,number,unit,zip;public Address(String country,String province,String city,String street,String number,String unit,String zip)this.country=country;this.province=province;this.city=city;this.stre
19、et=street;this.number=number;this.unit=unit;this.zip=zip;public void printAddress()System.out.println(国家:+country);System.out.println(地址:+province+city+street);System.out.println(门牌号码:+number);System.out.println(单位:+unit);System.out.println(邮编:+province);public String getCountry()return country;publ
20、ic String getAds()String str;str=province+city+street;return str;public String getNumber()return number;public String getUnit()return unit;public String getZip()return zip;class DomesticAds extends Addresspublic DomesticAds(String province,String city,String street,String number,String unit,String z
21、ip)super(中国,province,city,street,number,unit,zip);public void printAddress()public String printAddress(int x)String str;str=国内邮件n地址:+super.getAds()+n门牌号码:+super.getNumber()+n单位:+super.getUnit()+n邮编:+super.getZip();return str;class ForeignAds extends Addresspublic ForeignAds(String country,String pro
22、vince,String city,String street,String number,String unit,String zip)super(country,province,city,street,number,unit,zip);public void printAddress()public String printAddress(int x)String str;str=国外邮件nCountry :+super.getCountry()+nAddress:+super.getAds()+nNumber:+super.getNumber()+nUnit:+super.getUni
23、t()+nZip:+super.getZip();return str;public class AddressTest /* * param args */public static void main(String args) / TODO Auto-generated method stubAddress ads1=new Address(中国,江西省,南昌市,洪都大道,10栋1038号,洪都医院,);ads1.printAddress();DomesticAds ads2=new DomesticAds(天津市, ,大沽南路,1038号,天津科大,);System.out.println(ads2.printAddress(0);ForeignAds ads3=new ForeignAds(England,London, ,The fifth Street,64,London Hospital,);System.out.println(ads3.printAddress(0);(2)运行结果:专心-专注-专业
限制150内