《java平时作业.doc》由会员分享,可在线阅读,更多相关《java平时作业.doc(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-作者xxxx-日期xxxxjava平时作业【精品文档】作业一:/第二章75页1) 找出一个二维数组的鞍点,即该位置上的元素在该行上最大、在列上最小(也可能没有鞍点)。public class ArrayMaxMin public static void main(String args) int i,j,k,l,g; int two_array=10,6,5,1,2,4,16,7,11; for(i=0;itwo_array.length;i+) for(j=0;jtwo_array0.length;j+) l=0; g=0; for(k=0;ktwo_arrayik) +l; if(l=t
2、wo_array0.length-1) for(k=0;ktwo_array.length;k+) if(two_arrayijtwo_arraykj) +g; if(g=two_array.length-1) (该二维数组的鞍点为:two_array+(i+1)+(j+1)+=+two_arrayij); 2) 编程验证歌德巴赫猜想,任何大于6的偶数可以表示为两素数之和,如10=3+7。 public class GoldbachGuess public static void main(String args) int i,j,k,l,m,n=50; for(i=8;in;i+) /取大于
3、6小于n之间的任意偶数验证 if (i%2=0) for(j=2;ji;j+) for(l=1,m=0;l=j;l+) if(j%l=0) m=m+1; if(m=2) k=i-j; for(l=1,m=0;l=k;l+) if(k%l=0) m=m+1; if(m=2) (i+=+j+k+ ); 作业二: 例3-8/第三章187页对银行帐户类BankAccount进行一系列修改和测试声明BankAccount类声明toString()方法声明存取款方法使用DecimalFormat类声明类方法生成特殊的实例声明类变量作业三:/第四章122页编写java程序,程序中有一个父类Telephone
4、,类中包含电话品牌、电话号码、通话时间、费率等属性,以及计算话费和显示信息等方法;程序中还有一个子类Mobilephone,除了具有Telephone类的属性外,还有自己的属性如网络类型、被叫时间,同时它有自己的计算话费和显示信息的方法。最后程序中应包含一个主类来使用上述两个类并显示它们的信息。class Telephone private String telephone_brand,telephone_number; private int dialing_time;/通话时间以秒为单位。 private double telephone_price; public String getT
5、elephone_brand() return telephone_brand; public String getTelephone_number() return telephone_number; public int getDialing_time() return dialing_time; public double getTelephone_price() return telephone_price; public void setTelephone_brand(String brand) telephone_brand=brand; public void setTeleph
6、one_number(String number) telephone_number=number; public void setDialing_time(int time) dialing_time=time; public void setTelephone_price(double price) telephone_price=price; public double telephone_bills() int time_minter; double telephone_bills; if(dialing_time%60=0) time_minter=dialing_time/60;
7、else time_minter=dialing_time/60+1; telephone_bills=time_minter*telephone_price; return telephone_bills; public void call_information() (此次通话时间:+dialing_time/36000); (dialing_time/3600%10+:+dialing_time/600); (dialing_time/60%10+:+dialing_time%60/10); (dialing_time%60%10+ ;); class Mobilephone exten
8、ds Telephone private String net_type; private int called_time; boolean being_called; public String getNet_type() return net_type; public void setNet_type(String net_type) =net_type; public int setCalled_time(boolean y_n) being_called=y_n; if(being_called=false) called_time=getDialing_time(); return
9、called_time; else called_time=0; return called_time; public int getCalled_time() return called_time; public void call_information() if(called_time=0) (此次通话类型为:被叫。); else (此次通话类型为:主叫。); (此次通话时间为:+getDialing_time()/60+分+getDialing_time()%60+秒;); public double mobilephone_bills() double mobilephone_bil
10、ls; if(called_time=0) mobilephone_bills=(called_time/60)*getTelephone_price(); else mobilephone_bills=(called_time/60+1)*getTelephone_price(); return mobilephone_bills; public class PhoneTester public static void main(String args) Telephone a1=new Telephone(); Mobilephone a2=new Mobilephone(); a1.se
11、tTelephone_brand(Nokia); a1.setDialing_time(62); a1.setTelephone_price(0.2); (This phonebrand is +a1.getTelephone_brand()+;); (This phonenumber is +a1.getTelephone_number()+;); a1.call_information(); (此次通话费用为:+a1.telephone_bills()+;); (); a2.setTelephone_brand(HEDY); a2.setDialing_time(200); a2.setT
12、elephone_price(0.2); a2.setNet_type(3G); a2.setCalled_time(false); (This phonebrand is +a2.getTelephone_brand()+;); (This phonenumber is +a2.getTelephone_number()+;); (网络类型为+a2.getNet_type()+;); a2.call_information(); (此次通话费用为:+a2.mobilephone_bills()+;); (); 作业四:/第四章123页设计3个类,分别是学生类Student,本科生类Under
13、graduate,研究生类Postgraduate,其中Student类是一个抽象类,它包含一个学生的基本信息如姓名、平均成绩等,及一个计算课程成绩等级的抽象方法。类Undergraduate和Postgraduate是其子类,它们计算课程成绩等级的方法有所不同(自己设计)。设计一个学生数组,既能放本科生对象又能放研究生对象。编写测试类测试以上功能。class Student private String stuName; private double subScore,aveScore; public void setstu(String stuName,double subScore,do
14、uble aveScore) =stuName; =subScore; =aveScore; public String getstuName() return stuName; public double getaveScore() return aveScore; public double getsubScore() return subScore; public void scoreRank() if(subScore=90) (该课程成绩等级为:优。); else if(subScore=80) (该课程成绩等级为:良。); else if(subScore=70) (该课程成绩等级
15、为:中。); else if(subScore=60) (该课程成绩等级为:一般。); else (该课程成绩等级为:不及格。); class Postgraduate extends Student public void subScore() if(getsubScore()=95) (该课程成绩为5); else (该课程成绩为+(int)getsubScore()/20); class Undergraduate extends Student public void scoreRank() if(getsubScore()=85&getsubScore()=60) (还课程等级为B)
16、; else (该课程等级为C); public class StudentTester public static void main(String args) Student stu=new Student3; stu0=new Undergraduate(); stu1=new Postgraduate(); stu2=new Student(); stu0.setstu(test1,86.6,92); stu1.setstu(test2,93,91); stu2.setstu(test3,85,89.5); for(int i=0;i=0;i-) (i); return (); pub
17、lic static void wordStatistic(String source) int sum,i,j,count; String a = (s); sum=; int ss=new intsum; for(i=0;isum;i+) ssi=ai.length(); for(i=0;isum;i+) count=0; for(j=0;jb) upper=middle; else if(tempb) lower=middle; index=lower; if(upper-lower=1) break; if(b=aindex) index+=1; (b+存在的位置:+index); e
18、lse if(ba0) (b+应该排在的位置:+1); (插入后的序列:); c0=b; for(int i=1;ia.length+1;i+) ci=ai-1; else index+=2; (b+应该排在的位置:+index); (插入后的序列:); cindex-1=b; for(int i=0;iindex-1;i+) ci=ai; for(int i=index;ia.length+1;i+) ci=ai-1; for(int i=0;ia.length+1;i+) (ci+ ); public static void main(String args) String intput=
19、(请输入要排序的数个数:); int a=(intput); (输入要排序的数字:); Scanner Intarray=new Scanner(System.in); int b=new inta; for(int i=0;ib.length;i+) bi=(); int temp; for(int i=1;i-1&tempbj) bj+1 = bj; j-; bj+1=temp; (排序后:); for(int i=0;ia;i+) (bi+ ); (); arrayPosition(b); 【课件】作业八:/第五章180页例5-16:音像店的出租电影业务,在实际应用中,可能会通过以下方式
20、查找需要的电影:1.通过标题(title)查找电影;2.可将电影分成不同的类型(type),如喜剧片、悲剧片、战斗片等。在进行出租时,可在某一特定的类型中查找电影;3.查找包括某一演员(actor/actress)的电影。import .*; class Movie private String title, type; private Vector actors; public String getTitle() return title; public String getType() return type; public Vector getActors() return actors
21、; public void setTitle(String aTitle) title = aTitle; public void setType(String aType) type = aType; public Movie() this(?, ?); public Movie(String aTitle, String aType) title = aTitle; type = aType; actors = new Vector(); public String toString() return(Movie: + + title + ); public void addActor(S
22、tring anActor) (anActor); class MovieStore private Hashtable movieList, actorList, typeList; public Hashtable getMovieList() return movieList; public Hashtable getActorList() return actorList; public Hashtable getTypeList() return typeList; public MovieStore() movieList = new Hashtable(); actorList
23、= new Hashtable(); typeList = new Hashtable(); public String toString() return (MovieStore with + () + movies.); public void addMovie(Movie aMovie) (), aMovie); if (!() (),new Vector(); (Vector)().add(aMovie); for (int i=0; i().size(); i+) String anActor = (String)().get(i); if (!(anActor) (anActor,
24、 new Vector(); (Vector)(anActor).add(aMovie); private void removeMovie(Movie aMovie) (); (Vector)().remove(aMovie); if (Vector)().isEmpty() (); for(int i=0; i().size(); i+) String anActor = (String)().get(i); (Vector)(anActor).remove(aMovie); if (Vector)(anActor).isEmpty() (anActor); public void rem
25、oveMovie(String aTitle) if (aTitle) = null) (No movie with that title); else removeMovie(Movie)(aTitle); public void listMovies() Enumeration titles = (); while() (); public void listMoviesWithActor(String anActor) Enumeration someMovies = (Vector)(anActor).elements(); while() (); public void listMoviesOfType(String aType) Enumeration someMovies = (Vector)(aType).elements(); while() (); 作业十:/第七章100页用两个
限制150内