「java语言程序设计习题」java语言程序设计试题及答案

博主:adminadmin 2023-01-16 09:06:10 523

今天给各位分享java语言程序设计习题的知识,其中也会对java语言程序设计试题及答案进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java程序设计上机题,求答案

public class Point {

int x;

int y;

public Point() {

}

public Point(int x, int y) {

this.x = x;

this.y = y;

}

public Point(int x) {

this.x = x;

this.y = x;

}

public double distance() {//求当前点到原点的距离

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

}

public double distance(int x1, int y1) {//求当前点到(x1,y1)的距离

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

}

public double distance(Point other){

int x2 = other.x;

int y2 = other.y;

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

}

}

JAVA练习题!

1、i=-2

i=-1

i=1

2、big=Java Applet

3、The_value_is_10

4、20070101 张英 07计科二班

20070202 李瑞 07计科二班

5、10

6、false,true

26、A

27、D

28、B

29、C

30、B

31、B

好多编程题啊,有时间再说咯

Java程序设计题目

3, 文件名:Three.java

public class Three {

public static void main(String[] args) {

Student stu = new Student("Zhang San", true, (short)12);

System.out.println("Student name: " + stu.name);

System.out.println("Student is a male?: " + stu.sex);

System.out.println("Student's age: " + stu.age);

stu.work();

stu.study();

Teacher teacher = new Teacher();

teacher.learnMoney();

}

}

abstract class Person{//抽象类Person

protected String name;

protected boolean sex;

protected short age;

protected abstract void work(); //work抽象方法

}

interface Learnmoney{//Learnmoney接口

public void learnMoney();

}

interface Study{//Study接口

public void study();

}

class Student extends Person implements Study{//Student类

public void work() {

System.out.println("学生的工作是努力学习");

}

public Student(String name, boolean sex, short age){

super.name = name;

super.sex = sex;

super.age = age;

}

public void study() {

System.out.println("学生正在学习");

}

}

class Teacher extends Person implements Learnmoney{

public void work() {

System.out.println("教师的工作是教书育人");;

}

public void learnMoney() {

System.out.println("教师正在赚钱");

}

}

class Docotor extends Person implements Learnmoney{

public void work() {

System.out.println("医生的职责是救死扶伤");

}

public void learnMoney() {

System.out.println("医生正在赚钱");

}

}

------------------------------------

4文件名:Four.java

public class Four {

public static void main(String[] args) {

Rectangle r = new Rectangle(3, 4);

System.out.println("Area is : " + r.area());

System.out.println("Circle is: " + r.circle());

}

}

class Rectangle{

private double width;

private double height;

public Rectangle(double width, double height){

this.width = width;

this.height = height;

}

public double circle(){//求周长

return (width + height) * 2;

}

public double area(){//求面积

return width * height;

}

}

--------------------

5Five.java

public class Five {

public static void main(String[] args) {

AImpl a = new AImpl();

a.paint();

}

}

interface A {

public int method1(int x);

public int method2(int x, int y);

}

class AImpl implements A{

public int method1(int x) {

return (int)Math.pow(x, 5);

}

public int method2(int x, int y) {

return x y? x: y;

}

public void paint(){

int result1 = method1(2);

int result2 = method2(2, 8);

System.out.println("method1(2) = " + result1);

System.out.println("method2(2, 8) = " + result2);

}

}

-----------------------------测试

method1(2) = 32

method2(2, 8) = 8

《Java语言程序设计基础篇》第六版的练习题和编程题答案

哥们我给你写完了,耽误了我半个小时的时间啊!你直接运行就可以了

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Calendar;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Constellation implements ActionListener{

private JFrame frame = null;

private JTextField year = null;

private JTextField month = null;

private JTextField day = null;

private JLabel label1 = null;

private JLabel label2 = null;

private JLabel label3 = null;

private JPanel panel1 = null;

private JPanel panel2 = null;

private JButton button = null;

private JTextField output = null;

public static final String[] zodiacArr = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇",

"马", "羊" };

public static final String[] constellationArr = { "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座",

"狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };

public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22,

22 };

/**

* * 根据日期获取生肖 *

* @return 11.

*/

public static String date2Zodica(Calendar time) {

return zodiacArr[time.get(Calendar.YEAR) % 12];

}

/**

* * 根据日期获取星座 *

* @param time *

* @return

*/

public static String date2Constellation(Calendar time) {

int month = time.get(Calendar.MONTH);

int day = time.get(Calendar.DAY_OF_MONTH);

if (day constellationEdgeDay[month]) {

month = month - 1;

}

if (month = 0) {

return constellationArr[month];

}

// default to return 魔羯

return constellationArr[11];

}

public Constellation(){

frame = new JFrame("计算生肖,星座");

year = new JTextField("",3);

month = new JTextField("",3);

day = new JTextField("",3);

label1 = new JLabel("请输入年份:");

label2 = new JLabel(",请输入月份:");

label3 = new JLabel(",请输入日期:");

button = new JButton("查看结果");

button.addActionListener(this);

panel1 = new JPanel();

panel1.setLayout(new FlowLayout(FlowLayout.CENTER));

panel1.add(label1);

panel1.add(year);

panel1.add(label2);

panel1.add(month);

panel1.add(label3);

panel1.add(day);

panel1.add(button);

frame.setLayout(new BorderLayout());

frame.add(panel1,BorderLayout.NORTH);

panel2 = new JPanel();

output = new JTextField("",40);

panel2.add(output,JPanel.CENTER_ALIGNMENT);

frame.add(panel2,BorderLayout.CENTER);

frame.setSize(500, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

output.setText("");

int y = Integer.parseInt(year.getText());

int m = Integer.parseInt(month.getText());

int d = Integer.parseInt(day.getText());

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.YEAR, y);

calendar.set(Calendar.MONTH, m);

calendar.set(Calendar.DAY_OF_MONTH, d);

String zodica = date2Zodica(calendar);

String constellation = date2Constellation(calendar);

String str = "您输入的日期为:"+y+"年-"+m+"-月"+d+"日,得到的生肖:"+zodica+",星座:"+constellation;

output.setText(str);

}

//testcode

public static void main(String[] args) {

new Constellation();

}

}

JAVA语言程序设计两道练习题。谢谢!

第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。

问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。

第二题可以答一下:

package pillar;

public class Pillar { private Geometry buttom;

private double height;

public Pillar() {

// TODO Auto-generated constructor stub

}

public Pillar(Geometry button,double height){

this.buttom = button;

this.height = height;

}

public double getVolume(){

return this.buttom.getArea()*height;

}

public Geometry getButtom() {

return buttom;

}

public void setButtom(Geometry buttom) {

this.buttom = buttom;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public interface Geometry { double getArea();

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public class Circle implements Geometry { private double r;

public Circle() {

// TODO Auto-generated constructor stub

}

public Circle(double r) {

this.r = r;

}

public double getArea() { return Math.PI*r*r;

}

public double getR() {

return r;

}

public void setR(double r) {

this.r = r;

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public class Rectangle implements Geometry { private double width;

private double height;

public Rectangle() {

// TODO Auto-generated constructor stub

}

public Rectangle(double width, double height) {

this.width = width;

this.height = height;

}

public double getArea() { return this.width*this.height;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public class TestPillar {

/** * @param args

*/

public static void main(String[] args) {

Circle c = new Circle(5);

Rectangle r = new Rectangle(3,4);

Pillar p1 = new Pillar(c,6);

Pillar p2 = new Pillar(r,6);

System.out.println("圆的体积:"+p1.getVolume()+"\t矩形的体积:"+p2.getVolume());

}

}

求大量JAVA习题!!!急!!!(不是编程题)

注意:还不是完整的文档,太长了不能全帖完。要的话找我!^_^

《Java程序设计》练习题

一、判断题

1、 Java语言采用面向对象的思想编程,具有跨平台、分布式、多线程等优点。 ( )

2、 一个Java源程序可有多个类,但只仅有一个public类,而且程序名与public类名相同。 ( )

3、方法中的形参可以和方法所属类的属性同名。 ( )

4、接口无构造器,不能有实例,也不能定义常量。 ( )

5、利用File对象可以判断一个文件或目录是否存在。 ( )

6、JFrame,JPanel,JApplet和JButton四种组件都属于容器组件。 ( )

7、BorderLayout是面板的缺省布局管理器。 ( )

8、BorderLayout最多可以使用5个组件。 ( )

9、一个面板(JPanel)不能被加入另一个面板(JPanel)中。 ( )

10、菜单需要一个JMenuBar对象,以使他们能被添加到JFrame。 ( )

11、线程可以用yield使同优先级的线程运行。 ( )

12、System.in是标准输入流,能用read方法读取键盘的输入。 ( )

13、数据流就是数据通信通道,指在计算机的输入输出之间运动的数据序列。( )

二、填空题

1、设x,y,z的值分别为ture、false和false,试计算下列逻辑表达式的值:

(1) x y||!zture (2) !x||!y!z

(3) (!x!y)||(!y!z) (4) xy||true!z

2、求下面表达式的值:

(1) 已知x=2、y=6、z=5.0,求x+(int)y/2*z%4

(2) 已知x=123,求x/100+x%100/10+x%10

(3) 已知x=160、y=2.8、z=5,求(byte)x +(int)y+(float)z;

(4) 设 int x=17,y=5; 执行语句 x%=x++/--y 后x的值为 。

(5) 设 int a=7,b=6,c=5;,表达式 (a+b)(c*c)b==c||cb 的值为 。

(6) 设 int a=3,b=5,c=7;,表达式ac||cb!=0c==b+a 的值为 。

3、下列表达式中n和x被赋值为多少?

int n=0;

int x=1;

n=x++ + x++; //这里n= , x=

n=n++ - x--; //这里n= , x=

n=x-- + -x++; //这里n= , x=

n=++x + x++; //这里n= , x=

4、使用 方法为组件设置布局管理器,JFrame的缺省布局管理器是 ,内容面板的缺省布局管理器是 。

5、写出4个常见的异常例子: 、 、 和 。重新抛出一个异常用 语句。

6、线程通过 方法可以休眠一段时间,然后恢复运行,当 时,线程进入死亡状态。

7、编写一个线程可以用 和 来实现。

8、创建文件(c:\test.txt)对象的语句是 ,DataInputStream对象提供 方法可以按行读取文件内容。

9、Container 的________方法可以将_______组件加入容器。

10、在执行Java线程的程序中,程序通过调用_______方法启动线程,随后又调用________方法。

11、使用 方法为组件设置布局管理器,JFrame的缺省布局管理器是 ,内容面板的缺省布局管理器是 。

12、Java.swing.JFrame.getContentPane()的返回类型是 。

13、数据越界抛出的异常类是 ,整数除零抛出的异常类是 ,算术溢出抛出的异常类是 。

三、选择题

1、 下面哪些是不合法的变量名称? ( )

A) 2D B) True C) _name D) T1 E) while-ture

2、下列变量定义不正确的是: ( )

A) boolean status=false; B) float d = 45.6;

C) char c = “a”; D) int k = 1+’1’; E) float f=1/4;

3、下列数组的定义不合法的是: ( )

A) char c[][]=new char[2][3];

B) char c[][]=new char[6][];

C) char [][]c=new char[3][3];

D) char [][]c=new char[][4];

E) int []a[] = new int[10][10];

4、对于下列代码:

public class Example{

String str=new String("hello");

char ch[]={'d','b','c'};

public static void main(String args[]){

Example ex=new Example();

ex.change(ex.str,ex.ch);

System.out.println(ex.str+"and"+ex.ch[0]);

}

public void change(String str,char ch[]){

str="world";ch[0]= 'a';

}

}

输出结果是: ( )

A) hello and d B) hello and a

C) world and d D) world and a

5、下列说法哪个是正确的? ( )

A) 子类不能定义和父类同名同参数的方法

B) 子类只能继承父类的方法,而不能重载

C) 重载就是一个类中有多个同名但有不同形参和方法体的方法

D) 子类只能覆盖父类的方法,而不能重载

6、如果一个程序段中有多个catch,则程序会按如下哪种情况执行? ( )

A) 找到合适的例外类型后继续执行后面的catch

B) 找到每个符合条件的catch都执行一次

C) 找到合适的例外类型后就不再执行后面的catch

D) 对每个catch都执行一次

7、以下哪一项不是File类的功能: ( )

A) 创建文件

B) 创建目录

C) 删除文件

D) 拷贝文件

8、下列说法哪个是正确的? ( )

A) BorderLayout是面板的缺省布局管理器

B) 当鼠标指针位于一个GUI组件的边上时,发生一个MouseOver事件

C) 一个面板(Jpanel) 不能被加入到另一个面板(Jpanel)中

D) 在BorderLayout中,添加到NORTH区的两个按钮将并排显示。

9、在java程序中,下列关于线程的说法错误的是: ( )

A) run方法是运行线程的主体

B) 多个进程运行时执行顺序是按顺序执行的

C) 如果线程死亡,它便不能运行

D) 在java中,高优先级的可运行线程会抢占低优先级线程

10、关于JDBC访问数据库的说法错误的是: ( )

A) 建立数据库连接时,必须加载驱动程序,可采用Class.forName()实现

B) 用于建立与某个数据源的连接可采用DriverManager类的getConnection方法

C) 建立数据库连接时,必须要进行异常处理

D) JDBC中查询语句的执行方法必须采用Statement类实现

四、程序阅读题

1、阅读下面的程序,程序保存为Test.java:

1) public class Test

2) {

3) public static void main(String[] args)

4) {

5) System.out.println(args[2]);

6) }

7) }

以上程序经编译后用java Test 1 2 3 运行得到的输出结果是什么?

2、阅读下面的程序:

① public class Test{

② public static void main(String[] a){

③ int i = Integer.parseInt(a[0]);

④ switch (i) {

⑤ case 1:System.out.println("Frist season");break;

⑥ case 2:System.out.println("Second season");

⑦ case 3:System.out.println("3th season");break;

⑧ case 4:System.out.println("Last season");

⑨ }

⑩ }

⑪ }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,用java Test 2 运行得到的输出结果是什么?

3、阅读下面的程序:

① public class Test{

② public static void main(String[ ] args) {

③ int x,y=2,i=0,j=0;

④ if(args.length2) System.exit(-1);

⑤ x = Integer.parseInt(args[1]);

⑥ switch(x){

⑦ case 1:switch(y){

⑧ case 1:i++;break;

⑨ case 2:j++;break;

⑩ default:i++;j++;

⑪ }

⑫ case 2:i++;j++;break;

⑬ default:i++;j++;

⑭ }

⑮ System.out.println("i="+i);

⑯ System.out.println("j="+j);

⑰ }

⑱ }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,用java Test 1 2 3 运行得到的运行结果是什么?

4、阅读下面的程序,程序保存为Test.java:

1) public class Test

2) {

3) short mValue;

4) public static void main(String[] args)

5) {

6) int a = 32;

7) int b = 56;

8) Test os = new Test(a+b);

9) os.Show( );

10) }

11) protected Test(short aValue) { mValue = aValue; }

12) public void Show( ) { System.out.println(mValue); }

13) }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,运行结果是什么?

5、阅读下面的程序:

class test

{

public static void main(String[] args)

{

int i = 1;

int All = 0;

for (;i=10;i++)

{

if (i%6==0) break;

if(i%2==0) {i=i+2;continue;}

All = All + i;

}

System.out.println(All);

}

}

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,运行结果是什么?

6、阅读下面的程序,程序保存为Test.java:

1) public class Test

2) {

3) public static void main(String[] args)

4) {

5) int i = 100;

6) int j = 0;

7) boolean b = true;

8) while (b)

9) {

10) if (b||(i50)) b = false;

11) else b = true;

12) j=j+1;

13) i=i-1;

14) }

15) System.out.println(j);

16) }

17) }

上面的程序经编译,运行后输出的结果是什么?

7、阅读下面的程序:

1) public class test

2) {

3) public static void main(String argv[])

4) {

5) Bird b = new Bird();

6) b.Fly(3);

7) }

8) }

9) class Bird

10) {

11) static int Type = 2;

12) private void Fly(int an_Type)

13) {

14) Type = an_Type;

15) System.out.println("Flying..."+Type);

16) }

17) }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,运行结果是什么?

8、阅读下面的程序:

1) abstract class Base{

2) abstract public void myfunc();

3) public void another(){

4) System.out.println("Another method");

5) }

6) }

7) public class Abs extends Base{

8) public static void main(String argv[]){

9) Base b = new Abs();

10) b.another();

11) }

12) public void myfunc(){

13) System.out.println("My Func");

14) }

15) public void another(){

16) myfunc();

17) }

18) }

以上程序经编译后,运行结果是什么?

9、阅读下面的程序:

1) class Super{

2) public int i=0;

3) public Super(){

4) i=1;

5) }

6) }

7) public class Sub extends Super{

8) public Sub(){

9) i=2;

10) }

11) public static void main(String args[]){

12) Sub s=new Sub();

13) System.out.println(s.i);

14) }

15) }

上面的程序经编译后,运行结果是什么?

10、阅读下面的程序,程序保存为Test.java:

1) public class Test {

2) public static void main(String[ ] args) {

3) int index=0;

4) while (index=100) {

5) index+=10;

6) if (index==40)

7) break;

8) System.out.println("The index is "+index);

9) }

10) }

11) }

上面的程序经编译,运行后输出的结果是什么?

五、问答题

1、 设int类型变量x和y分别初始化为3和100,下列语句的循环体共执行几次?执行完下列语句后x和y的值分别是多少?

(1) while(x=y) x=2*x;

(2) while(y/x5) if(y-x25) x=x+1; else y=y/x;

(3) do{x=2*x;} while(xy);

(4) do{x=y/x;y=y-x;}while(x=y);

(5) do{y=y/x-1; if(y=x) y=x;} while(y=1);

2、简述对象、类的概念。

3、简述封装、继承性和多态性的概念。

4、根据以下的设计要求编写java源代码。

类名: Circle

实例数据(均为private):

radius(double) //圆的半径

方法:

构造方法(没有参数,设置radius的值为10.0)

setRadius(有一个double参数,将radius的值设为这个新值),

getRadius (没有参数,返回radius的值)

sameSize(有一个参数,是另一个Circle对象的引用,如果两个Circle对象的radius的差小于0.001,则返回true)

5、下面的代码使用上题的Circle类,请回答下面的问题:

public class CircleTester {

public static void main(String[] args) {

Circle c1,c2,c3;

c1 = new Circle();

c2 = new Circle();

System.out.println(“are same is: “+c1.sameSize(c2));

c2.setRadius(20.0);

compare.reset();

System.out.println(“are same is: “+c1.sameSize(c2));

}

}

问题:

(1) 共创建了几个Circle对象?

(2) 程序运行的结果是什么?

6、假设已有一个ArrayMethods 类,包含以下的方法:

public static void replace(double[] a, int p, double v)

将数组a中下标为p的元素的值替换为v.

public static void fill(double[] a, int p, double v)

将数组a中的前p个元素用value值填充(即将数组a的前p个元素的值,设成v),如果数组的长度小于p,则全部元素都设成v

public static void display(double[] a)

在屏幕上显示数组a的内容

现在假设你在main()方法中已经声明了如下变量:

double[] array1={4.5, 6.0, 0.1, 2.2};

double[] array2;

double num1;

int pos=3;

要求对下面的3组语句,先回答是否合法(即没有编译错误),如果是合法的,请描述程序运行的效果,如果不合法,请说明理由.

1) num1 = array1[2];

ArrayMethods.replace(array1, pos, num1);

ArrayMethods.display(array1);

2) num1 = array1[0];

ArrayMethods.fill(array1, pos, num1);

ArrayMethods.display(array1);

3) num1 = array1[1];

array2=new double[num1];

ArrayMethods.display(array2);

六、编程题

1、分别利用for、while、do~while编写计算正整数n1到n2的累加和。

2、编写一个编程,给定一个t的值(t的值也可通过命令行输入),按下式计算y值并输出,要求分别写作if语句和switch语句。

t2-1 0≤t<1

t3-2•t-2 1≤t<3

y= t2-t•sint 3≤t<5

t+1 5≤t<7

t-1 其它

3、设计一个类TestArraySum,定义一个含有10个元素的int类型数组a,10个数组元素的值是11~20,再定义一个方法arraySum(int[] a),返回数组所有元素的和,最后用main方法实现在屏幕上输出数组a所有元素的和。

4、编写一个java程序Suansu.java,定义两个整型变量a和b,使用构造函数初始化a为10,b为5,并定义求出a与b的和(方法名为AddAB)、差(方法名为SubAB)、积(方法名为MultiAB)、商 (方法名为DivAB)的方法。

用另一个java程序TestSuansu.java测试Suansu.java定义的方法,并在屏幕上输出结果。

5、创建一个名为Rectangle的类来表示一个使用宽度和高度来改变量的矩形,矩形的宽度和高度由构造方法来确定。为Rectangle类创建下列方法:

 getArea返回矩形的面积,要求长和高的范围为0~50;

 getPerimeter返回矩形的周长;

 Draw使用星号(*)作为描绘字符画出该矩形(假设宽度和高度为整数);

在另一个类TestRectangle中编写main方法来测试Rectangle类。

6、用面向对象的思想定义一个接口Area,其中包含一个计算面积的方法CalsulateArea(),然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法CalsulateArea(),分别计算圆和矩形的面积,最后写出测试以上类和方法的程序。

7、创建一个Frame,有两个Button按钮和一个TextField,点击按钮,在TextField上显示Button信息。

8、创建下图的GUI程序(注意:不需要提供任何功能)。

9、编写一个文件拷贝的程序,将文件C:\test1.txt的内容拷贝到C:\test2.txt中。

10、编写一个程序,统计给定文件中每个字母出现的频率。

11、编写一个程序,统计给定文件中包含的单词数目,并按单词表的顺序显示统计结果。

12、用图形界面设计一个简单的计算器。

13、用图形界面实现简单的银行柜台业务,包含创建新帐户、取款、存款、查询帐户余额等业务。

关于java语言程序设计习题和java语言程序设计试题及答案的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。