㈠ java程序设计基础题
选c;
第一个输出的是(5, 3),应该没有问题吧,
第二个输出的是(3, 5) ,是witchCoords()函数里的输出语句,在witchCoords()函数里的x和y实现了交换,所以是3和5;
第三个输出的是(5, 3),因为传递给switchCoords()函数的值是数据值。switchCoords()里x和y的值改变不会影响实参!
也就是说,传数据值形参的改变不会影响实参;而传地址值形参的改变会影响实参。
如果传递的是数组,实参会随着形参而改变。
㈡ JAVA基础编程题~
public class student{
private String name;
private int age;
public student(String name,int age){ //student的带有String,int带参数的构造函数
this.name = name;
this.age = age; //把传进来的参数赋值给创建出来的类的对象
}
public static void main(String[] args){
student s = new student("zhangsan",18); //调用student的构造函数创 造一个对象s
System.out.println("姓名:"+s.name+"年龄:"+s.age);//调用对象的属性就用s.name s.age!
}
}
----------------------------------------------------------------------
public class Student {
static String name;//设定String类型的静态变量name
static int age;//设定int类型的静态变量age
public static void main(String[] args){//main为主方法,运行从main开始
name="zhangsan";//给name赋值zhangsan
age=18;//给age赋值18
System.out.println("该学生的名字为"+name+"年龄为:"+age);//输出name和age
}
}
static 修饰的属性和方法只属于这个类本身,而不属于这个类所创建出来的对象,所以可以直接写name,age 而不用像上面那种要先创建个类的对象出来 !
㈢ java编程题,基础
packagecom.sunpx.test;
publicclassSong{
protectedStringname;
protectedStringcomposer;
protectedintyear;
publicSong(Stringname,Stringcomposer,intyear){
this.name=name;
this.composer=composer;
this.year=year;
}
publicSong(Stringname){
this.name=name;
}
@Override
publicStringtoString(){
return"Song{"+
"name='"+name+'''+
",composer='"+composer+'''+
",year="+year+
'}';
}
publicStringtoString(intyear){
StringyearN=String.valueOf(year);
return"Song{"+
"name='"+name+'''+
",composer='"+composer+'''+
",year="+yearN+
'}';
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetComposer(){
returncomposer;
}
publicvoidsetComposer(Stringcomposer){
this.composer=composer;
}
publicintgetYear(){
returnyear;
}
publicvoidsetYear(intyear){
this.year=year;
}
}
㈣ JAVA程序设计试题
DCCBBAAAD
㈤ 用JAVA编程 类与对象的基础题
class Phone{
private String phonenumber;
public void setPhonenumber(String phonenumber){
this.phonenumber=phonenumber;
}
public String getPhonenumber(){
return phonenumber;
}
public void recCall(){
System.out.println("接到一个电话");
}
public void telCall(){
System.out.println("拨出一个电话");
}
}class Fixedphone extends Phone{
private String phonenumber;//号码是私有,设置为private,不可继承
public void recCall(){
System.out.println("以"+this.phonenumber+"呼出了一个电话"); //重载了父类的recCall
}
}class Cordlessphone extends Fixedphone{
private String phonenumber;
public void info(){
System.out.println("这是无绳电话的信息");
}
}interface Moveable{
public void moveinfo();
}class Mobilephone extends Phone implements Moveable{
private String phonenumber;
public void moveinfo(){
System.out.println("我实现了可移动性");
}
}public class PhoneTest{
public static void main(String a[]){
Phone[] p=new Phone[5];
Phone p1=new Phone();
p1.setPhonenumber("123456789");
p[0]=p1;
Phone p2=new Phone();
p2.setPhonenumber("987654321");
p[1]=p2;
Mobilephone mp=new Mobilephone();
mp.setPhonenumber("11111");
p[2]=mp;
Fixedphone fp=new Fixedphone();
fp.setPhonenumber("22222");
p[3]=fp;
Cordlessphone cp=new Cordlessphone();
cp.setPhonenumber("33333");
p[4]=cp;
for(int i=0;i<p.length;i++){
System.out.println(p[i].getPhonenumber());
} p[4]=p[1];
System.out.println(p[4].getPhonenumber());
}} 写的不是很好,希望对你有帮助噶
㈥ 帮忙做一个Java程序设计题
//接口
publicinterfaceShape{
();
publicabstractdoublegetArea();
}
//矩形类
{
privatedoublewidth=0;//宽
privatedoubleheight=0;//长
privatedoublearc=0;//对角线
publicMyRect(doublewidth,doubleheight){
this.width=width;
this.height=height;
}
@Override
publicdoublegetPeremeter(){
return(width+height)*2;
}
@Override
publicdoublegetArea(){
returnwidth*height;
}
publicvoidshow(){
arc=Math.sqrt((width*width)+(height*height))
System.out.println("长:"+height+"宽:"+width+"面积:"+getArea()+"对角线:"+arc);
}
}
publicclasstest{
publicstaticvoidmain(String[]args){
MyRectmyRect=newMyRect(20,30);
System.out.println("周长:"+myRect.getPeremeter());
System.out.println("面积:"+myRect.getArea());
myRect.show();
}
}
㈦ 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基础编程题求解,请写的基础一点不然根本看不懂啊
importjava.util.Scanner;
/*
*@authorww
*
*/
publicclassTest{
publicstaticvoidmain(String[]args){
Scanners=newScanner(System.in);
System.out.println("输入一个正整数n(n>=2)");
intn=s.nextInt();
StringBuildersb=newStringBuilder();
booleanhas=false;
for(inti=1;i<n;i++){
inttemp=i;
sb.append(i).append("");
for(intj=i+1;j<n;j++){
temp+=j;
sb.append(j).append("");
if(temp==n){
System.out.println(sb.toString());
has=true;
}
}
sb.delete(0,sb.length());
}
if(!has){
System.out.println("NONE");
}
}
}
㈨ 求Java基础,练习题。
选择题 (25道)
1. 下列选项中,不属于Java语言特点的一项是( C )。
A:分布式 B:安全性 C:编译执行 D:面向对象
2. Java语言的特点与 C/C+ +语言的比较中说法错误的是:( D )
A:简单性Java继承了 C/C+ +的语法 ,丢弃了其中不常用又容易引起混淆的功能。
B:Java是一种纯面向对象的语言 ,具有封装、继承 ( Inheritance)和多态( Polymorphism)的特点。
C:Java应用程序可凭借URL打开并访问网络上的对象。
D:解释型Java写成的源代码需要被编译成高阶的字节码 ,它们与机器架构有关。
3. 阅读下列代码,选出该代码段正确的文件名( C )。
class A{
void method1(){
System.out.println("Method1 in class A");
}
}
public class B{
void method2(){
System.out.println("Method2 in class B");
}
public static void main(String[] args){
System.out.println("main() in class B");
}
}
A: A.java B:A.class C: B.java D: B.class
4. 如果一个类的文件名为Student.java,但是类的代码为:
public class Student {
public static void main(String[] args) {
System.out.println(8>>2);
}}
那么下列说法正确的是:( B )
A:程序运行结果为8; B:程序运行结果为2;
C:程序运行结果为0; D:程序编译错误,不能运行;
5. 符合对象和类的关系的是( D )。
A:教师和学生 B:书和房子
C:狗和猫 D:飞机和交通工具
6. 关于垃圾回收机制描述不正确的是( B )
A:垃圾回收机制不须通过程序员调用相应方法,也能自动启动。
B:java程序员用System.gc()方法一定能进行垃圾回收;
C:垃圾回收机制属于jvm自动操作,java程序员可以不进行垃圾回收操作。
D:垃圾回收机制并不是由操作系统自动执行。
7. 编译下面源程序会得到哪些文件( D )?
class A1{
}
class A2 exdends A1{
}
public class B{
public static void main(String[] args){
}
}
A: 只有B.class文件 B:只有A1.class和A2.class文件
C: 编译不成功 D:A1.class、A2.class和B.class文件
8. 下列关于基本数据类型的说法中,不正确的一项是( D )。
(A)boolean类型变量的值只能取真或假
(B)float是带符号的32位浮点数
(C)double是带符号的64位浮点数
(D)char是8位Unicode字符
9. 下列(D )是合法的标识符?
A:12class B:void C:-5 D:_blank
10. 在编写Java程序时,如果不为类的成员变量定义初始值,Java会给出它们的默认值,下列说法中不正确的一个是( D )。
A:byte的默认值是0 B:boolean的默认值是false
C: char类型的默认值是’\0’ D: long类型的默认值是0.0L
11. 下列程序执行的结果是:( B )
public class News {
public static void main(String[] args) {
System.out.println(1+2+ "aa"+3);
}}
A: "12aa3" B: "3aa3 " C: "12aa" D: "aa3"
12. 表达式(12==0) && (1/0 < 1)的值为( B )。
A: true B: false C: 0 D: 运行时抛出异常
13. 下列循环体执行的次数是( C )。
int y=2, x=4;
while(--x != x/y){ }
A : 1 B: 2 C : 3 D : 4
14. 已知如下代码:
switch(m){
case 0: System.out.println("Condition 0");
case 1: System.out.println("Condition 1");
case 2: System.out.println("Condition 2");
case 3: System.out.println("Condition 3");break;
default:System.out.println("Other Condition");
}
当m的值为( D )时,输出“Condition 3”
(A)2 (B)0、1 (C)0、1、2 (D)0、1、2、3
15. 下列语句输出的结果是:( C )
public class X3 {
public static void main(String[] args) {
for(int i=0; i<10; i++){
if(i==5) break;
System.out.print(i);
}
}
}
A:编译错误; B:1234;C:01234;D:12345;
16. 下列语句输出的结果是:( D )
public class Lx1 {
public static void main(String[] args) {
for(int i=0;i<5;i++){
switch(i){
case 0:System.out.print("B");
case 1:System.out.print("e");break;
case 2:System.out.print("g");
case 3:System.out.print("!");break;
case 4:System.out.print("!");break;
default:System.out.print("!");
}
}
}
}
A:Beg!!! B:Beeg! C:Beg! D:Beeg!!!
17. 下面foreach循环的程序输出结果是( D )。
public class Lx1{
public static void main(String[] args) {
String s1[]={"欢迎您","3","G","同","学",};
Arrays.sort(s1);
for(String s0:s1)
System.out.print (s0);
}
}
A:欢迎您3G同学 B:3G欢迎您同学 C:同学欢迎您3G D:3G同学欢迎您
18. 阅读以下程序,选择正确的运行结果:( B )
public class Lx1 {
public static void main(String[] args) {
byte d[]="YOUIHE你我他".getBytes();
String s=new String(d,6,2);
System.out.println(s);
}
}
A:HE; B:你; C:我; D:他
19. 设有下列数组定义语句:
int a[][]= {{1, 2}, {3}};
则对此语句的叙述正确的是( D )。
A: 定义了一个名为a的一维数组 B: a数组 a[1][1]为0
C: a数组元素的下标为1~3 D: 数组中每个元素的类型都是整数
20. 下列程序输出的结果是:( B )
public class Lx1 {
public static void main(String[] args) {
String a[][] ={{"","","",""},{""},{"",""}};
System.out.println(a[2].length);
}
}
A:1 B:2 C:3 D:4
21. 关于以下程序的说明,正确的是( C )
1. class StaticStuff
2. {
3. static int x=10;
4. static { x+=5;}
5. public static void main(String args[ ])
6. {
7. System.out.println(“x=” + x);
8. }
9. static { x/=3;}
10. }
A、4行与9行不能通过编译,因为缺少方法名和返回类型
B、9行不能通过编译,因为只能有一个静态初始化器
C、编译通过,执行结果为:x=5
D、编译通过,执行结果为:x=3
22. 给出下面代码,关于该程序以下哪个说法是正确的?( C )
public class Person{
static int arr[] = new int[5];
public static void main(String a[]) {
for(int i=0;i
System.out.print(arr[0]);
}
}
A、编译时将产生错误 B、编译时正确,运行时将产生错误 C、输出零 D、输出空
23. 下面程序中类ClassDemo中定义了一个静态变量sum,分析程序段的输出结果。( C )
class ClassDemo {
public static int sum=1;
public ClassDemo() {
sum = sum + 5;}
}
public class ClassDemoTest{
public static void main(String args[]) {
ClassDemo demo1=new ClassDemo();
ClassDemo demo2=new ClassDemo();
System.out.println(demo1.sum);}
}
A: 0 B: 6 C: 11 D: 2
24. 下面关于方法的说法,不正确的是( C )。
A: Java中的构造方法名必须和类名相同
B: 方法体是对方法的实现,包括变量声明和合法语句
C: 如果一个类定义了构造方法,也可以用该类的默认构造方法
D: 类的私有方法不能被其他类直接访问
25. 在Java中下列说法正确的是( C )
A) 一个子类可以有多个父类,一个父类也可以有多个子类
B) 一个子类可以有多个父类,但一个父类只可以有一个子类
C) 一个子类可以有一个父类,但一个父类可以有多个子类
D) 上述说法都不对