java大题

更新时间:2024-01-30 22:19:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

目录

目录................................................................................................................................................... 1 二(1)求球的体积。 ..................................................................................................................... 3 二(2)求矩形周长和面积。 ......................................................................................................... 3 三(1)从键盘输入一整数,根据是奇数还是偶数分别输出odd和even。 ............................. 4 三(2)输入3个整数,从小到大输出。 ..................................................................................... 5 第五章............................................................................................................................................... 6 1.编写一个代表三角形的类,其中,3条边为三角形的属性,并封装有求三角形的面积和周长的方法。分别针对3条边为3,4,5和7,8,9的两个三角形进行测试。 ................................... 6 5_2 编写一个学生类Student,包含的属性有学号、姓名、年龄,将所有学生 ...................... 7 五、(3)编写一个Person类,其中包括人的姓名、性别、年龄、子女等属性,并封装有获得姓名、获得年龄、增加1岁、获得子女、设置子女等方法,其中,子女为一个Person数组。................................................................................................................................................. 10 5-4.编写一个代表日期的类,其中有代表年月日的三个属性,创建日期对象时要判断参数提供的年月日是否合法,不合法要进行纠正,年默认值为2000;月的值在1到12之间,默认值为1;日由一个对应12个月的整型数组给出合法值,特别的,对于二月,通常为28天,但闰年的二月最多29天,该年的值为400的倍数,或者为4的倍数但不为4的倍数但不为100的倍数的年份为闰年。将创建的日期对象输出时,年月日之间用“/”分隔。 ............. 12 5-5 编写一个矩阵类,其中封装有一个代表矩阵的二维数组,并提供一个实现两个相同行、列的矩阵的相加方法。利用随机函数产生两个3行4列的矩阵,验证类设计。 ................. 14 五、(6)创建简单的银行账户类。 ............................................................................................. 16 5-7(7)n只猴子要选大王,选举方法如下’:所有猴子按1,2,?,n编号并按照顺序围 .. 18 六(1)给point类添加以下几个求两点间距的多态方法,并进行调用测试。 ..................... 20 六(2)定义person类,子类student类。 ................................................................................. 25 六(3)circle类,求圆面积。 ..................................................................................................... 27 六(4)复数类,测试两复数的加、乘、求模方法。 ............................................................... 30 8(1)定义一个接口,包含一个Display()方法用于显示信息:通知类,汽车类,广告类?? ........................................................................................................................................................ 33 8(2)定义Shape,其中包括一个方法size(),设计矩形、圆、圆柱体等,实现Shape接口。 ........................................................................................................................................................ 35 8(3)定义一个抽象类水果,包括getweight()方法。 .............................................................. 37 8(4)定义Startstop接口,含有start()和stop()两个方法。分别创建会议和汽车两个类实现Startstop接口。......................................................................................................................... 40 (1)classname是否在java类库中。 .............................................................................................. 41 (2) 学生姓名,学号,成绩。 ...................................................................................................... 42 1、创建一个名为TestApp的java应用程序,在屏幕上分行显示如下一段文字: ............... 45 华东交通大学 ................................................................................................................................. 45 欢迎您! ......................................................................................................................................... 45 2、创建一个名为TestApplet的Applet程序,显示“两个同心圆,园内显示两个汉字‘同心’”,并编写相应的text.html文件。 .................................................................................................... 45 * ...................................................................................................................................................... 46

***................................................................................................................................................... 46 *****............................................................................................................................................... 46 1、球的体积计算公式为:4/3πr^3,编写一个程序输入半径,求体积。 ............................... 48 2、输入矩形的长和宽,计算矩形的周长和面积。 ................................................................... 48 3、从键盘输入摄氏温度C,计算华氏温度F的值并输出,其转换公式如下:F=(9/5)*C+32 ........................................................................................................................................................ 49 4、从键盘输入一个实数,获取该实数的整数部分,并求出实数与整数部分的差,将结果分别用两种形式输出:一种是直接输出,另一种是精确到小数点后4为的浮点格式输出。 . 50 1、从键盘输入一个整数,根据是奇数还是偶数分别输出“odd”和“even”。 .................... 50 2、从键盘输入3个整数,按由小到大的顺序排列输出。 ....................................................... 51 3、从键盘输入a、b、c共3个整数,计算方程ax^2+bx+c=0的根。 ................................... 52 4、运输公司对用户计算运费,路程越远,折扣越高,标准如下: ....................................... 54 5、利用下式求e^x的近似值。 ................................................................................................... 56 6、设有一条绳子,长2000米,每天剪去三分之一,计算多少天后长度变为1厘米。 ..... 57 7、计算n至少多大时,一下不等式成立: ............................................................................... 57 8、编写一个程序,实现从键盘输入10个整数,将最大、最小的整数找出来并输出。 ..... 58 9、百鸡百钱问题。公鸡每只3元,母鸡每只5元,小鸡三只1元,用100元钱买100只鸡,公鸡、母鸡、小鸡应各买多少? ................................................................................................. 59 10、用二重循环输出九九乘法表(注意用制表符“\\t”实现结果的对其显示)。 ................. 60 11、 输入一个整数,判断该数是否为降序数,是则输出ture,否则输出false。降序数是指该数的各位数字从高到低逐步下降(包括相等)。 ................................................................... 60 7_ 1统计字符串中a的出现次数 ................................................................................................. 61 7_2键盘输入若干行文字 .............................................................................................................. 62

二(1)求球的体积。

import javax.swing.*; public class V {

public static void main(String args[]){

String s=JOptionPane.showInputDialog(\请输入球的半径:\ double r=Double.parseDouble(s); double V=Math.PI*r*r*r*3/4;

System.out.printf(\球的体积=%.3f\ } }

二(2)求矩形周长和面积。

import javax.swing.*; public class area {

public static void main(String args[]){

String x=JOptionPane.showInputDialog(\请输入矩形的长:\ String y=JOptionPane.showInputDialog(\请输入矩形的宽:\

double l=Double.parseDouble(x); double d=Double.parseDouble(y); double c=4*(d+l); double area=d*l;

System.out.printf(\矩形的周长=%.3f\ System.out.printf(\矩形的面积=%.3f\} }

三(1)从键盘输入一整数,根据是奇数还是偶数分别输出odd和even。

import javax.swing.*; public class number {

public static void main(String args[]){ int a; String s;

s=JOptionPane.showInputDialog(\请输入一个整数:\a=Integer.parseInt(s); if(a%2==0)

System.out.printf(\else

System.out.printf(\

} }

三(2)输入3个整数,从小到大输出。

import javax.swing.*; public class rank {

public static void main(String args[]){ int a,b,c,t;

String x=JOptionPane.showInputDialog(\请输第一个整数:\a=Integer.parseInteger(x);

String y=JOptionPane.showInputDialog(\请输第二个整数:\b=Integer.parseInteger(y);

String z=JOptionPane.showInputDialog(\请输第三个整数:\c=Integer.parseInteger(z); if(a>b) t=a; a=b; b=t; if(b>c) t=b; b=c;

c=t; if(a>c) t=a; a=c; c=t;

Syatem.out.printf(\ } }

第五章

1.编写一个代表三角形的类,其中,3条边为三角形的属性,并封装有求三角形的面积和周长的方法。分别针对3条边为3,4,5和7,8,9的两个三角形进行测试。

Public class Triangle{ Private double a,b,c;

Public Triangle(double a,double b,double c){ This.a=a; This.b=b; This.c=c;}

Public double area(){

Double s=(a+b+c)/2.0;

Return Math.sqrt(s*(s-a)*(s-b)*(s-c));} Public double circle(){ Return a+b+c;} Public String toString(){

Return “三角形(”+a+”,”+b+”,”+c+”)”; }

Public static void main(String args[]){ Triangle t1=new Triangle(3,4,5);

System.out.println(t1+”的面积为:”+t1.area()); System.out.println(t1+”的周长为:”+t1.circle()); Triangle t2=new Triangle(7,8,9);

System.out.println(t2+”的面积为:”+t2.area()); System.out.println(t2+”的周长为:”+t2.circle()); } }

5_2 编写一个学生类Student,包含的属性有学号、姓名、年龄,将所有学生

存储在一个数组中,自拟数据,用数组的初始化方法给数组赋值,并实现如下操作

1>将所有学生的年龄增加1岁; 2>按数组中顺序显示所有学生的信息。 3>查找显示所有年龄大于20岁的学生的名单。 */

public class Student { int num; int age; String name;

public String toString() { return\学号:\,姓名:\,年龄: }

public Student(int Num,int Age,String Name) { num=Num; age=Age; name=Name;

}

public static void main(String args[]) {

Student s1=new Student(3,18,\张三\\

}

Student s2=new Student(1,21,\小路\Student s3=new Student(33,20,\Student s4=new Student(13,20,\Student s5=new Student(8,17,\Student s[]={s1,s2,s3,s4,s5};

System.out.println(\班级学生名单如下:\output(s);

for(int i=0;i

s[i].age++;

System.out.println(\所有学生年龄加1后...\output(s); int count=0;

for(int i=0;i

if(s[i].age>=20)

count++;

System.out.println(\大于20岁人数是:\

static void output(Student s[]) { }

for(int i=0;i

System.out.println(s[i]);

}

五、(3)编写一个Person类,其中包括人的姓名、性别、年龄、子女等属性,并封装有获得姓名、获得年龄、增加1岁、获得子女、设置子女等方法,其中,子女为一个Person数组。

用某实际数据测试该类的设计。 public class Person{ private String name; private int age;

private Person[]children=null;

public Person(String myname,int myage){ name=myname; age=myage; }

public Person(String name1,int age1,Person chs[]){ name=name1; age=age1; children=chs; }

public String toString(){

String s=\ s+=\ return s; }

public String getName(){ return name; }

public String getAge(){ return age; }

public void incAge(){ age++; }

public Person[] getChidren(){ return children; }

public void setChildren(Person[] mychildren){ children=mychildren; }

public static void main(String args[]){ Person p1=new Person(\ Person p2=new Person(\

Person[]c1={p1,p2};

Person p3=new Person(\

System.out.println(\ Person[]mychild=p3.getChildren(); for (int k=0;k

System.out.println(\} } }

5-4.编写一个代表日期的类,其中有代表年月日的三个属性,创建日期对象时要判断参数提供的年月日是否合法,不合法要进行纠正,年默认值为2000;月的值在1到12之间,默认值为1;日由一个对应12个月的整型数组给出合法值,特别的,对于二月,通常为28天,但闰年的二月最多29天,该年的值为400的倍数,或者为4的倍数但不为4的倍数但不为100的倍数的年份为闰年。将创建的日期对象输出时,年月日之间用“/”分隔。

Public class Date{

Private int year=2000; Private int month=1; Private int day;

Public Date (int theYear,int theMonth,int theDay){ If(theMonth>0&&theMonth<=12) month=theMonth; else month=1; year=theYear;

day=checkDay(theDay); }

Private int checkDay(int testDay){

Int daysPerMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};

If(month==2&&testDay<=29&&(year@0==0||(year%4==0&&year0!=0))) Return testDay;

If(testDay>0&&testDay<=daysPerMonth[month-1]) Return year+”/”+month+”/”+day; }

Public String toString(){

Return year+”/”+month+”/”+day;} Public static void main(String args[]){

Date d=new Date(2010,3,29); System.out.println(“日期为:”+d); Date m=new Date(2010,2,30); System.out.println(“日期为:”+m); } }

5-5 编写一个矩阵类,其中封装有一个代表矩阵的二维数组,并提供一个实现两个相同行、列的矩阵的相加方法。利用随机函数产生两个3行4列的矩阵,验证类设计。

public class Matrix{ int m; int n; int [][] num;

public Matrix(int m,int n){ this.m=m; this.n=n;

num=new int[m][n]; }

public void initialMatrix(){ for(int i=0;i

num[i][j]=(int)(Math.random()*10); } }

public static Matrix plus(Matrix a,Matrix b){ if(a.m!=b.m||a.n!=b.n){ return null; } else{

Matrix c=new Matrix(a.m,a.n); for(int i=0;i

c.num[i][j]=a.num[i][j]+b.num[i][j]; return c; } }

public void output(){ for(int i=0;i

for(int j=0;j

System.out.print(num[i][j]+\ System.out.println(); } }

public static void main(String[] args){ Matrix m1=new Matrix(3,4); Matrix m2=new Matrix(3,4); m1.initialMatrix(); m2.initialMatrix(); Matrix m3=plus(m1,m2); m1.output();

System.out.println(\ m2.output();

System.out.println(\ m3.output(); } }

五、(6)创建简单的银行账户类。

public class BankUser{

private String accout; private String name; privat double money;

public Bankuser(String accout,String name,double money){ this.account=account; this.name=name; this.money=money; }

public double query(){ return money; }

public void save(double x){ money +=x; }

public double draw(double x){ if(money>x){ money-=x; return money; }else{ return -1; }

public static void main(String[] args){

BankUser user1=new BankUser(\ BankUser user2=new BankUser(\张三\ System.out.println(\额:\ user1.save(100); user1.draw(45); user1.draw(500); System.out.println(\额:\ Systom.out.println(\额:\ } }

:\

:\

:\

5-7(7)n只猴子要选大王,选举方法如下’:所有猴子按1,2,?,n编号并按照顺序围

成一圈,从第k个猴子起,由l开始报数,报到m时,该猴子就跳出圈外,下一只猴子再

次由l开始报数,如此循环,直到圈内剩下一只猴子时,这只猴子就是大王。

1. 输入数据:猴子总数n,起始报数的猴子编号k,出局数字为m

2. 输出数据:猴子的出队序列和猴子大王的编号。 public class Monkey{ int number;

public Monkey(int num)? number=num; )

public static void main(String[】args){ int n=32; int m=3;

Monkey mon[1=new Monkey[n]; for (int k=O;k

System.out.print(mon[j].number+” ”): mon[j] = null; p=0:

k++; } j_(j+1)%n; }

System.out.p~intln(): for(int i=0;i

System.out.println(“King is”一+mon[i].number); } }

六(1)给point类添加以下几个求两点间距的多态方法,并进行调用测试。

class Point{ int x; int y;

public Point(int x,int y){

this.x=x;

this.y=y; }

public double distance(Point p){

return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); }

public double distance(int x,int y){

return Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y)); }

public static double distance(Point x,Point y){

return Math.sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));

} }

public class hc {

public static void main(String args[]){

Point p=new Point(3,9);

Point a=new Point(7,6);

System.out.println(p.distance(a));

System.out.println(p.distance(5,8));

System.out.println(Point.distance(p,a)); }

class Person{

String name;

char sex; int age;

public Person(String name,char sex,int age){

this.name=name;

this.sex=sex;

this.age=age; } }

class teacher extends Person{

String pro;

String partement;

public teacher(String name,char sex,int age,String pro,String partement){

super(name,sex,age);

this.pro=pro;

this.partement=partement; }

public String toString(){

return name+\ } }

六(2)定义person类,子类student类。

class student extends Person{

String num;

String time;

String zhuanye;

public student(String time,String zhuanye){

super(name,sex,age);

this.num=num;

this.time=time;

this.zhuanye=zhuanye;

name,char sex,int age,String num,String

}

public String toString(){

return name+\ } }

public class hc {

public static void main(String args[]){

student a=new student(\张三\网络工程\

System.out.println(\学生基本信息:\

teacher b=new teacher(\李四\教师\数信学院\

System.out.println(\教师信息:\

} }

六(3)circle类,求圆面积。

class Circle{

double r;

public Circle(double r){

this.r=r; }

public double area(){

return Math.PI*r*r; }

public static double area(double r){

return Math.PI*r*r; }

public static double area(Circle c){

return Math.PI*c.r*c.r; }

}

public class hc {

public static void main(String args[]){

Circle c1=new Circle(3);

System.out.println(c1.area());

System.out.println(Circle.area(3));

System.out.println(Circle.area(c1)); } }

六(4)复数类,测试两复数的加、乘、求模方法。

public class Hc {

private double x,y;

public Hc(double a,double b){ x=a; y=b; }

public String toString (){

return \ }

public Hc add(Hc a){

return new Hc(x+a.x,y+a.y); }

public Hc add(double a,double b){

return new Hc(x+a,y+b); }

public Hc multiply(Hc a){

return new Hc(x*a.x,y*a.y); }

public Hc multiply(double a,double b){

return new Hc(x*a,y*b); }

public double getvalue(){

return Math.sqrt(x*x+y*y); }

public static void main(String args[]){

Hc x,y,z;

x=new Hc(1,2);

y=new Hc(2,4);

z=x.add(y);

System.out.println(\

z=x.add(4,5);

System.out.println(\

z=x.multiply(y);

System.out.println(\

z=x.multiply(5,6) ;

System.out.println(\

double m=x.getvalue();

System.out.println(x+\的模=\ } }

8(1)定义一个接口,包含一个Display()方法用于显示信息:通知类,汽车类,广告类??

interface Display{

void display(); }

class Inform implements Display{ public void display(){

System.out.println(\通知内容\ } }

class Car implements Display{ public void display(){

System.out.println(\汽车油亮\ } }

class Adervise implements Display{ public void display(){

System.out.println(\广告消息\ } }

public class Java1{

public static void main(String[]args){

Display []s={new Inform(),new Car(),new Adervise()}; for (int i=0;i

} }

8(2)定义Shape,其中包括一个方法size(),设计矩形、圆、圆柱体等,实现Shape接口。

interface Shape{ double size(); }

class Line implements Shape{ private double x1,y1,x2,y2;

public Line(double x1,double y1,double x2,double y2){ this.x1=x1;this.y1=y1; this.x2=x2;this.y2=y2; }

public double size(){

return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); } }

class Circle implements Shape{ private double r; public Circle(double r){

this.r=r; }

public double size(){ return 3.14*r*r; } }

class Conference implements Shape{ private double peoples; public Conference(double p){ peoples=p; }

public double size(){ return peoples; } }

class 圆柱体 implements Shape{ private Circle c; private double h;

public 圆柱体(Circle c1,double h1){ c=c1;h=h1; }

public double size(){

return c.size()*h; } }

public class Java2 {

public static void main(String args[]){ Shape s[]=new Shape[4]; s[0]=new Line(22,56,41,57); s[1]=new Circle(47); s[2]=new Conference(247); s[3]=new 圆柱体(new Circle(20),4); for (int z=0;z

8(3)定义一个抽象类水果,包括getweight()方法。

abstract class Fruit{

abstract public double getWeight(); }

class Apple extends Fruit{ private double weight;

public Apple(double weight){ this.weight=weight; }

public double getWeight(){ return weight; } }

class Peach extends Fruit{ private double weight; public Peach(double weight){ this.weight=weight; }

public double getWeight(){ return weight; } }

class Orange extends Fruit{ private double weight; public Orange(double weight){ this.weight=weight; }

public double gerWeight(){

return weight; } @Override

public double getWeight() {

// TODO Auto-generated method stub return 0; } }

public class Java3 {

public static void main(String[]args){ Fruit s[]=new Fruit[3]; s[0]=new Apple(3); s[1]=new Peach(2); s[2]=new Orange(1); for(int k=0;k

System.out.println(s[k].getClass().getName()+\ } }

8(4)定义Startstop接口,含有start()和stop()两个方法。分别创建会议和汽车两个类实现Startstop接口。

interface StartStop{ void Start(); void Stop(); }

class Conference implements StartStop{ public void Start(){

System.out.println(\ }

public void Stop(){

System.out.println(\} }

class car implements StartStop{ public void Start(){

System.out.println(\ public void Stop(){

System.out.println(\ } }

public class Java4 {

public static void main (String[] args){ StartStop s[]={new car(),new Conference()}; for(int i=0;i

(1)classname是否在java类库中。

public static void main(String[] args) { String classname; if (args.length>0) classname=args[0]; else

classname=\ try{

Class c= Class.forName(classname);

System.out.println(classname+\是java类库中已有的类\

}

catch(ClassNotFoundException e) {

System.out.println(classname+\不在java类库中\ } } }

(2) 学生姓名,学号,成绩。

package shiyan; import javax.swing.*; public class std {

public static void main(String args[]) { String name[]=new String[10]; int xuehao[]=new int[10]; double chengji1[]=new double[10]; double chengji2[]=new double[10]; double chengji3[]=new double[10]; double average[]=new double[10];

for(int i=1;i<=10;i++)

{name[i-1]=(String) JOptionPane.showInputDialog(\输入第\个学生的姓名\

xuehao[i-1]=Integer.parseInt(JOptionPane.showInputDialog(\输入第\个学生的学号\

chengji1[i-1]=Integer.parseInt(JOptionPane.showInputDialog(\输入第\个学生的成绩1\

chengji2[i-1]=Integer.parseInt(JOptionPane.showInputDialog(\输入第\个学生的成绩2\

chengji3[i-1]=Integer.parseInt(JOptionPane.showInputDialog(\输入第\个学生的成绩3\

average[i-1]=(chengji1[i-1]+chengji2[i-1]+chengji3[i-1])/3; }

double sum=0; for(int i=1;i<=10;i++)

sum=sum+average[i-1];

double zongaverage=sum/10;

System.out.println(\总的平均分数为\ for(int i=1;i<=10;i++)

{System.out.println(\第\个学生的姓名为\学

号为\第一科成绩为\chengji1[i-1]+\第二科成绩为\第三科成绩为\平均成绩为\ } } } 第一章: 一、问答题

1、简述面向对象程序设计的特点

答:面向对象程序设计有4大特性:封装、继承、多态、

抽象。

2、简述java语言的特点。

答:java语言有面向、跨平台与解释执行、健壮和安全、

多线程、面向网络、动态性等特点。 二、编程题

1、创建一个名为TestApp的java应用程序,在屏幕上分行显示

如下一段文字:

华东交通大学

欢迎您!

public class TestApp{

public static void main(String args[]){ System.out.println(\华东交通大学\ System.out.println(\欢迎您!\ } }

2、创建一个名为TestApplet的Applet程序,显示“两个同心圆,

园内显示两个汉字‘同心’”,并编写相应的text.html文件。

import java.applet.Applet; import java.awt.*; pubilc class TestApplet{

public void paint(Graphics g){

本文来源:https://www.bwwdw.com/article/fumw.html

Top