jv程序设计习题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
J A V A 程序设计》练习
题
写出下面程序的运行结果
1、import java.io.*;
public class abc
{
public static void main(String args [ ])
{
AB s = new AB("Hello!","I love JAVA.");
}
} 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] ;
}
}
运行结果:S=180
//****3 、import java.io.* ;
public class abc
{
public static void main(String args[ ])
{
}
}
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()
{
}
} 运行结果:a=60 b=20 c=30
4、以下程序的输出结果为_Peter is17 years old !___________ 。
public class Person {
String name;
int age;
public Person(String name, int age) { = name; this.age = age;
public static void main(String[] args) { Person c = new Person("Peter", 17);
}
}
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() {
课程号:" + 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;
体重:" + weight + " 斤");
} private void out() {
public static void main(String[] args) { = "汤姆猫"; Tom cat = new Tom(); cat.setWeight(20); cat.out();
}
}
7、以下程序的输出结果_姓名:Tom 年龄:15 家庭住址:金水区电话:学校:九中
public class Father {
String name, address, tel;
int age;
public Father(String name, int age) { = name; this.age = age;
}
void out() {
姓名:" + name);
年龄:" + age);
}
void outOther() { 家庭住址:" + address); 电话:" + tel);
}
}
}