Java 读程序
结果
1、class V1{
int a=1;
void m1(){ System.out.println(a); }
}
class E1 extends V1{
int a=2,b=3,c;
void m2(){ System.out.println(a); }
}
class H{
public static void main(String[] s){
E1 e1=new E1();
e1.m1(); e1.m2(); }
}
结果:
1
2
2、class V1{
public int x=2;
V1(){ System.out.println("V1"); }
}
class V2 extends V1{
public int x=3;
V2(){ System.out.println("V2"); }
}
class H{
public static void main(String[] s){
V1 e1=new V2();
System.out.println(e1.x); } }
结果:
V1
V2
2
//*V1 e1=new V2();等价成:
V1 e1;
e1=newV2(); 这样就很明显了
3、import java.io.*;public class abc{ public static void main(String args[ ]) { AB s = new AB("Hello!"); System.out.println(s.toString( )); }} class AB { String s1; String s2; AB( String str1 , String str2 ) { s1 = str1; s2 = str2; } public String toString( ) { return s1+s2;}}
以上程序编译错误:参数不匹配
改为:
import java.io.*;
public class Test
{ public static void main(String args[ ])
{ AB s = new AB("Hello","world!");
System.out.println(s.toString( ));
}
}
class AB {
String s1;
String s2;
AB( String str1 , String str2 )
{ s1 = str1; s2 = str2; }
public String toString( )
{ return s1+s2;}
}
结果为:Helloworld!
4、 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);} }
结果:180
5、 class V1{
public int x=4;
V1(){ System.out.println("V1"); }
}
class V2 extends V1{
public int x=7;
V2(){ System.out.println("V2"); }
}
class H{
public static void main(String[] s){
V1 e1=new V2();
System.out.println(e1.x); }
}
结果:
V1
V2
4
6、class V1{
int a=3;
void m1(){ System.out.println(a); }
}
class E1 extends V1{
int a=5,b=3,c;
void m2(){ System.out.println(a); }
}
class H{
public static void main(String[] s){
E1 e1=new E1();
e1.m1(); e1.m2(); }
}
结果:
3
5
1、int i= 1, j= 10 ;
do{
if (i++> --j) continue;
} while (i<5);
System.out.println(“i=”+i+”j=”+j); 简单的java程序的考查
答:i=5 j=6
2、 import java.io.*;
public class abc
{
public static void main(String args[])
{
String s1 = "Hello!";
String s2 = new String("I like Java!");
System.out.println(s1+" "+s2);
}
} 简单的java中的类的考查。
答: Hello! I like Java!
3、int i=9;
switch(i){
default:System.out.println(“default”);
case 0: System.out.println(“zero”);
break;
case 1: System.out.println(“one”);
case 2: System.out.println(“two”);
} switch语句的考查
答:default
zero
4.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 ( i%3 = = 0 ) s += a[i] ;
System.out.println("s="+s);
}
}
答:s = 120
5、public class Waiter {
int var;
Waiter(int var) {
this("Welcome");
}
Waiter(String s) {
this();
System.out.println(s);
}
Waiter() {
System.out.println("Good-bye");
}
public static void main(String[] args) {
Waiter t = new Waiter(1);
}
}
答:Good-bye
Welcome
功能
1、public class H{
public static void main(String args[]){
Fact N=new Fact(4);
System.out.print(N.fact());
} }
class Fact{
int n;
Fact(int nn) {n=nn;}
int fact(){
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
} }
功能:计算n!(n的阶乘)
2、public class H{
public static void main(String args[]){
int i,Max,Min;
int a[]={12,67,8,98,23,56,124,55,99,100};
Max=a[0];Min=a[0];
for(i=1; i<a.length;i++){
if(a[i]<Min) Min=a[i];
if(a[i]>Max) Max=a[i];
}
System.out.print(Max+" "+Min);
} }
功能:查找数组a的最大值和最小值
3.import java.io.*; public class abc { public static void main(String args[]) { SubClass sb = new SubClass( ); System.out.println(sb.max( )); } } class SuperClass { int a = 10 , b = 20 ; } class SubClass extends SuperClass { int max( ) { return ((a>b)?a:b); } }
功能:输出两个数中较大的
3、import java.io.* ;
public class abc { public static void main(String args[ ]) { int i , n = 10 , max = 0 , min = 0 , temp = 0; try { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); max = min = Integer.parseInt(br.readLine( )); } catch ( IOException e ) { } ; for ( i = 2 ; i <= n ; i ++ ) { try { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); temp = Integer.parseInt(br.readLine( )); if (temp > max ) max=temp; if (temp < min) min=temp; } catch ( IOException e ) { } ; } System.out.println("max="+max+"\nmin="+min); }}
功能:从控制台输入10个数,程序选出最大的和最小的
5、public class Sum{ public static void main( String args[ ]) { double sum = 0.0 ; for ( int i = 1 ; i <= 100 ; i + + ) sum += 1.0/(double) i ; System.out.println( "sum="+sum ); }
功能:计算1/1+1/2+……+1/100
6、import java.io.* ; public class Reverse { public static void main(String args[ ]) { int i , n =10 ; int a[ ] = new int[10]; for ( i = 0 ; i < n ; i ++ ) try { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); a[i] = Integer.parseInt(br.readLine( )); // 输入一个整数 } catch ( IOException e ) { } ; for ( i = n-1 ; i >= 0 ; i ―― ) System.out.print(a[i]+" "); System.out.println( ); } }
功能:实现对输入的10个数排序
回答问题
1.现有类说明如下,请回答问题:
public class A
{
String str1=" Hello! \t";
String str2=" How are you? ";
public String toString( )
{ return str1+str2; }
}
public class B extends A
{
String str1="\b\b,Bill.";
public String toString( )
{ return super.str1+str1; }
}
问题:
1)类A和类B是什么关系?(继承关系)
2)类A和类B都定义了str1属性和方法toString( ), 这种现象分别称为什么?(属性的隐藏;方法的覆盖)
3)若a是类A的对象,则a.toString( )的返回值是什么?
(Hello! How are you?)
4)若b是类B的对象,则b.toString( )的返回值是什么?(Hello,Bill.)
2.现有一个类定义如下,请回答问题:
class Employee
{
String name;
int age;
double wage;
static int No=0;
Employee(String a1,int a2,double a3)
{
name=a1; age=a2; wage=a3;
No++;
}
}
在使用该类时,已使用下面语句生成了该类的对象:
Employee e1,e2;
e1=new Employee("王劲",26,6300);
e2=new Employee("张山",30,3800);
问题:
1)e2.name,e2.age,e2.wage的值各是什么?(张山;30;3800.0)
2)生成对象e1、e2后,e1.No值为多少?能否通过类名做前缀引用属性No?(2; 能)
3.阅读程序,回答问题。
public class InheritTest1
{
public static void main (String[] args)
{
A aa; B bb;
aa=new A( ); bb=new B( );
aa.show( ); bb.show();
}
}
class A
{
int a=1;
double d=2.0;
void show( )
{ System.out.println("Class A: "+"\ta="+a +"\td="+d); }
}
class B extends A
{
float a=3.0f;
String d="Java program.";
int b=4;
void show( )
{
System.out.println("Class A: "+"\ta="+super.a +"\td="+super.d);
super.show( );
System.out.println("Class B: "+"\ta="+a +"\td="+d+"\tb="+b);
}
}
问题:1)这是哪一类java程序?(java应用程序)
2)类A和类B是什么关系?(类B是类A的子类)
3)按程序输出的格式写出程序运行后的结果.
(程序运行结果如下:
Class A: a=1 d=2.0
Class A: a=1 d=2.0
Class A: a=1 d=2.0
Class B: a=3.0 d=Java program. b=4 )
4.现有类说明如下,请回答问题:
class A
{
int x=10;
int getA(){return x;}
}
class B extends A
{
int x=100;
int getB(){return x;}
}
问题:1)类B是否能继承类A的属性x?(能)
2)若b是类B的对象,则b.getB()的返回值是什么?(100)
3)若b是类B的对象,则b.getA()的返回值是什么?(10)
4)类A和类B都定义了x属性,这种现象称为什么?
(属性的隐藏)
5.有如下源程序,请回答问题:
class A
{ String s="class A"; }
class B extends A
{ String s="class B"; }
public class TypeConvert
{
public static void main(String args[])
{
B b1,b2=new B();
A a1,a2;
a1=(A)b2;
a2=b2;
System.out.println(a1.s);
System.out.println(a2.s);
b1=(B)a1;
System.out.println(b1.s);
System.out.println(b2.s);
}
}
问题: 该程序的四行输出各是什么?
(class A
class A
class B
class B)
6.现有类说明如下,请回答问题:
public class A
{
int x=888;
String str="I like: ";
public String toString()
{ return str+x; }
}
public class B extends A
{
String x="java";
public String toString()
{ return str+x+" and "+super.x; }
}
问题:1)类A和类B是什么关系?(类B是类A的子类)
2)类A和类B都定义了x属性和方法toString(),这种现象分别称为什么?(属性的隐藏和方法的覆盖)
3)若a是类A的对象,则a.toString( )的返回值是什么?(I like: 888)
4)若b是类B的对象,则b.toString( )的返回值是什么?
(I like: java and 888)
7.运行类C的输出结果是什么?
class A
{
public A()
{
System.out.println(“The default constructor of A is invoked”);
}
}
class B extends A
{
public B()
{
}
}
public class C
{
public static void main(String[] args)
{
B b = new B();
}
}
8.阅读下列程序写出输出结果:
class A
{ String s="class A";
void show()
{
System.out.println(s);
}
}
class B extends A
{ String s="class B";
void show()
{
System.out.println(s);
}
}
public class TypeConvert
{
public static void main(String args[])
{
B b1;
B b2=new B();
A a1,a2;
a1=(A)b2;
a2=b2;
System.out.println(a1.s);
a1.show();
System.out.println(a2.s);
a2.show();
b1=(B)a1;
System.out.println(b1.s);
b1.show();
System.out.println(b2.s);
b2.show();
}
}
答案:
class A
class B
class A
class B
class B
class B
class B
class B
1.下面是一个类的定义,根据题目要求回答以下问题.
class B
{
private int x;
private char y;
public B(int i,char j)
{
x=i; y=j;
}
public void show()
{
System.out.println("x="+x+"; y="+y);
}
public void methodC(int x)
{
this.x=this.x+x;
y++;
show();
}
}
(1)定义类B的一个对象b,将类中的变量x初始化为10、变量y初始化为’A’,请写出相应的语句。(B b=new B(10,’A’);)
(2)若在(1)问基础上有方法调用语句:
b.show();
则输出如何?(x=10; y=A)
(3)若在(1)问基础上增加语句: b.methodC(1); 则输出为何?(x=11; y=B)
(x=11; y=B)
2.阅读程序,回答问题。
public class Test52
{
String static str1="Hello, Java world! \t";
String static str2="Hello, students! ";
public static void main(String args[])
{ System.out.print(str1); System.out.println(str2); }
}
问题:1)这是哪种形式的 java 程序 ?(java应用程序)
2)程序的输出是什么?
(Hello, Java world! Hello, students! )
3. 写出下列程序的输出结果
public class Test
{
public static void main(String[] args)
{
Count myCount = new Count();
int times = 0;
for(int i=0;i<100;i++)
increment(myCount , times);
System.out.println(“count is” + myCount.count);
System.out.println(“time is”+ times);
}
public static void increment(Count c , int times)
{
c.count++;
times++;
}
}
class Count
{
public int count;
Count(int c)
{
count =c;
}
Count()
{
count =1;
}
}
答案:
count 101
times 0
5.阅读下面程序,回答问题:
public class Foo
{
int i;
static String s;
void imethod()
{
}
static void smethod()
{
}
}
设f是Foo的一个实例,下列语句正确吗?
System.out.println(f.i);
System.out.println(f.s);
f.imethod();
f.smethod();
System.out.println(Foo.i);
System.out.println(Foo.s);
Foo.imethod();
Foo.smethod();
答案:
System.out.println(f.i);
答案: 正确
System.out.println(f.s);
答案: 正确
f.imethod();
答案: 正确
f.smethod();
答案: 正确
System.out.println(Foo.i);
答案: 错误
System.out.println(Foo.s);
答案: 正确
Foo.imethod();
答案: 错误
Foo.smethod();
答案: 正确
6.下列程序的输出结果是什么?
public class Foo
{
static int i=0;
static int j=0;
public static void main(String[] args)
{
int i=2;
int k=3;
{
int j=3;
System.out.println(“i + j is ”+ i + j);
}
k = i +j;
System.out.println(“k is ”+k);
System.out.println(“j is ”+ j);
}
}
答案:
i + j is 23
k is 2
j is 0
7. 根据下面的程序,指出下面每个元素的作用域(类作用域或块作用域)
变量x
变量y
方法cude
变量 i
变量 yPos
public class CubeTest
{
int x;
public void print()
{
int yPos = 10;
for(x=1;x<=10;x++)
{
System.out.println(cude(x));
for(int i=1;i<=yPos;i++)
System.out.println(" ");
}
}
public int cude (int y)
{
return y*y*y;
}
}
答案:
变量x : 类作用域
变量y : 块作用域
方法cude : 类作用域
变量 I : 块作用域
变量 yPos : 块作用域
1.阅读下面程序回答问题:
import java.io.*;
public class Class1
{
public static void main(String args[])
{
int a=5;
int b=0;
System.out.println(a/b);
try
{
System.out.println("a="+a);
System.out.println(a/b);
System.out.println("a*a="+a*a);
}
catch(ArithmeticException e)
{ System.out.println("除数为0,这是不行的!"); }
finally
{ System.out.println("finally被执行!"); }
System.out.println("异常已发生,但不影响程序的执行!");
}
}
1)运行上述程序,输出结果是什么?(异常提示信息除外)
答案:除提示异常提示信息外,无其他输出
2)将变量b的初值改成5后,输出结果是什么?
答案:
1
a=5
1
a*a=25
finally被执行!
异常已发生,但不影响程序的执行!