资源描述
《JAVA程序设计》练习题
写出下面程序的运行结果
1、 import java.io.*;
public class abc
{
public static void main(String args [ ])
{
AB s = new AB("Hello!","I love JAVA.");
System.out.println(s.toString( ));
}
}
class AB {
String s1;
String s2;
public AB(String str1, String str2)
{
s1 = str1;
s2 = str2;
}
public String toString( )
{
return s1+s2;
}
}
运行结果: Hello!I love JAVA
2、 import java.io.* ;
public class abc
{
public static void main(String args[ ])
{ int i, s = 0 ;
int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };
for ( i = 0 ; i < a.length ; i ++ )
if ( a[i]%3 = = 0 ) s += a[i] ;
System.out.println("s="+s);
}
}
运行结果: S=180
//****3、import java.io.* ;
public class abc
{
public static void main(String args[ ])
{
System.out.println("a="+a+"\nb="+b);
}
}
class SubClass extends SuperClass
{ int c;
SubClass(int aa, int bb, int cc)
{
super(aa, bb);
c=cc;
}
}
class SubSubClass extends SubClass
{ int a;
SubSubClass(int aa, int bb, int cc)
{ super(aa, bb, cc);
A = aa+bb+cc;
}
void show()
{
System.out.println("a="+a+"\nb="+b+"\nc="+c);
}
}
运行结果:a=60 b=20 c=30
4、以下程序的输出结果为_Peter is17 years old!________________。
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Person c = new Person("Peter", 17);
System.out.println(c.name + " is " + c.age + " years old!");
}
}
5、以下程序的输出结果为__课程号:101 课程名:ASP 学分:3___________________。
public class Course {
private String cNumber;
private String cName;
private int cUnit;
public Course(String number, String name, int unit) {
cNumber = number;
cName = name;
cUnit = unit;
}
public void printCourseInfo() {
System.out.println("课程号:" + cNumber + " 课程名:" + cName + " 学分:" + cUnit);
}
}
class CourseTest {
public static void main(String[] args) {
Course c;
c = new Course("101", "ASP", 3);
c.printCourseInfo();
}
}
6、以下程序的输出结果为__汤姆猫体重:20.0斤___________________。
public class Tom {
private float weight;
private static String name;
public void setWeight(float weight) {
this.weight = weight;
}
private void out() {
System.out.println(name + "体重:" + weight + "斤");
}
public static void main(String[] args) {
Tom.name = "汤姆猫";
Tom cat = new Tom();
cat.setWeight(20);
cat.out();
}
}
7、以下程序的输出结果_姓名:Tom 年龄:15 家庭住址:金水区 电话:_66123456 学校:九中_______________。
public class Father {
String name, address, tel;
int age;
public Father(String name, int age) {
this.name = name;
this.age = age;
}
void out() {
System.out.print("姓名:" + name);
System.out.print(" 年龄:" + age);
}
void outOther() {
System.out.print(" 家庭住址:" + address);
System.out.print(" 电话:" + tel);
}
}
class Son extends Father {
String school;
public Son(String name, int age) {
super(name, age);
}
void out() {
super.out();
super.outOther();
System.out.println(" 学校:" + school);
}
public static void main(String args[]) {
Son son = new Son("Tom", 15);
son.address = "金水区";
son.school = "九中";
son.tel = "66123456";
son.out();
}
}
8、下列程序的运行结果是____1 23 4 5_________________。
public class MyClass {
int a[] = { 1, 2, 3, 4, 5 };
void out() {
for (int j = 0; j < a.length; j++)
System.out.print(a[j] + "");
}
public static void main(String[] args) {
MyClass my = new MyClass();
my.out();
}
}
程序填空题
1.public class Sum{
public static void main(String [] args){
int j=10;
System.out.println("j is : "+j);
calculate(j);
System.out.println("At last, j is : "+j);
}
static void calculate (int j){
for (int i = 0;i<10;i++)
j++;
System.out.println("j in calculate() is: "+j);
}
}
输出结果为:
j is : (1) 10
j in calculate() is : (2) 20
At last j is : (3) 10
2. 按要求填空
abstract class SuperAbstract{
void a(){…}
abstract void b();
abstract int c(int i);
}
interface AsSuper
{
void x();
}
abstract class SubAbstract extends SuperAbstract implements AsSuper
{
public void b(){…}
abstract String f();
}
public class InheritAbstract extends SubAbstract{
public void x(){…}
public int c(int i ) {…}
public String f(){…}
public static void main(String args[]){
InheritAbstract instance=new InheritAbstract();
instance.x();
instance.a();
instance.b();
instance.c(100);
System.out.println(instance.f());
}
}
在以上这段程序中:
抽象类有:SuperAbstract和 (1) (写出类名)SubAbstract
非抽象类有: (2) (写出类名)InheritAbstract接口有: (3) (写出接口名)AsSuper
AsSuper中的x()方法是 抽象 (4) 方法,所以在InheritAbstract中必须对它进行 覆盖和实现 (5)
3. 按注释完成程序
public class Leaf {
private int i = 0; //此属性值用于检验
Leaf increment(){ //定义方法increment(),返回值是Leaf类的对象
i++;
return this (1) ;//将当前对象的地址作为返回值返回
}
void print() {
System.out.println(" i = " + i);
}
public static void main(String args[]){
Leaf x = new Leaf() (2) ; //创建Leaf类的对象x
x.increment().increment().increment().print();
}//多次调用方法increment(),返回的都是x的地址,i 值表示调用次数
}
输出结果为 i = 3 (3)
程序阅读题
1、阅读下面的程序代码,并回答问题(u问3分,v问3分,共6分)。
String s1 = new String("abcde");
String s2 = new String("abcde");
boolean b1= s1.equals(s2);
boolean b2 = s1== s2;
System.out.print(b1+" "+b2);
u程序段执行后,在命令行的输出结果如何?答:True false
v解释输出(1)的结果的原因?略
2、阅读下面的程序,并回答问题(u问3分,v问3分,共6分)。
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException {
BufferedReader buf=new BufferedReader(
new InputStreamReader(System.in));
while(true) {
String str = buf.readLine();
if(str.equals("quit"))
break;
int x=Integer.parseInt(str);
System.out.println(x*x);
}
}
}
编译运行上面的程序:
u从键盘输入10,回车后输出的结果如何?答:100
v从键盘输入exit,回车后程序能正确执行吗?为什么?答:不能,略
3、阅读下面的程序,回答问题(u问3分,v问3分,共6分)。
import java.awt.*;
import javax.swing.*;
public class T extends JFrame {
public T ( ) {
super("GridLayout");
Container con=this.getContentPane();
con.setLayout(new GridLayout(2,3));
con.add(new JButton("a"));
con.add(new JButton("b"));
con.add(new JButton("c"));
con.add(new JButton("d"));
con.add(new JButton("e"));
con.add(new JButton("f"));
setSize(200, 80);
setVisible(true);
}
public static void main(String args[]) {
new T();
}
}
u画图表示程序运行后的图形界面。 略
v如果程序通过实现某个接口处理按钮的动作事件,则该接口名为何?接口中的方法头声明如何? 略
4、阅读下面的程序,回答问题(u问3分,v问3分,共6分)。。
import java.util.*;
public class T {
public static void main(String args[]) {
Set set = new TreeSet();
set.add(new Integer(10));
set.add(new Integer(5));
set.add(new Integer(15));
set.add(new Integer(5));
set.add(new Integer(10));
System.out.println("size = " + set.size());
Iterator it=set.iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
}
}
u程序运行后输出的结果如何?答:Size=3 5 10 15
v说明java中的集合(Set接口)和映射(Map接口)的主要区别。 略
5、阅读下面的程序,并回答问题(u问3分,v问3分,共6分)。
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException {
BufferedReader buf=new BufferedReader(
new InputStreamReader(System.in));
while(true) {
String str=buf.readLine();
if(str.equals("quit"))
break;
int x=Integer.parseInt(str);
System.out.println(x*x);
}
}
}
编译运行上面的程序:
u从键盘输入5,回车后输出的结果如何?答:25
v从键盘输入quit,回车后程序执行情况如何? 答:终止应用程序的运行
程序设计题
1、按以下要求编写程序
(1) 创建一个Rectangle类,添加width和height两个成员变量
(2) 在Rectangle中添加两种方法分别计算矩形的周长和面积
(3) 编程利用Rectangle输出一个矩形的周长和面积
解答:
2、编写一个Frame框架应用程序,要求如下:
(1) 在窗口设置两个菜单“文件”、“编辑”
(2) 在“文件”菜单里添加三个菜单项“打开”、“保存”、“关闭”
(3) 在“编辑”菜单里添加两个菜单项“复制”、“粘贴”
(4) 点击关闭菜单项时,使程序关闭。
展开阅读全文