Java面向对象(上)练习

1.定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer  

银行账户类Account有方法:

(1)getInfo(),返回String类型,返回卡的详细信息

(2)取钱方法withdraw(),参数自行设计,如果取钱成功返回true,失败返回false

(3)存钱方法save(),参数自行设计,如果存钱成功返回true,失败返回false

  

其中Customer类有姓名、身份证号、联系电话、家庭地址等属性

    Customer类有方法say(),返回String类型,返回他的个人信息。

在测试类Bank中创建银行账户类对象和用户类对象,并设置信息,与显示信息

答:

Customerd对象

public class Customer {
   
private String name;
    private
String indenitiyCard;
    private
String phone;
    private
String sex;

    public
String getName() {
       
return name;
   
}

   
public void setName(String name) {
       
this.name = name;
   
}

   
public String getIndenitiyCard() {
       
return indenitiyCard;
   
}

   
public void setIndenitiyCard(String indenitiyCard) {
       
this.indenitiyCard = indenitiyCard;
   
}

   
public String getPhone() {
       
return phone;
   
}

   
public void setPhone(String phone) {
       
this.phone = phone;
   
}

   
public String getSex() {
       
return sex;
   
}

   
public void setSex(String sex) {
       
this.sex = sex;
   
}

   
public String say(){
       
return "name="+name+",indenitiyCard="+indenitiyCard+",phone="+phone+",sex="+sex;
   
}

   
public Customer(String name, String indenitiyCard, String phone, String sex) {
       
this.name = name;
        this
.indenitiyCard = indenitiyCard;
        this
.phone = phone;
        this
.sex = sex;
   
}
}

账户类:

public class Account {

    private String cid;

    private double balance;

    private Customer customer;



    public String getCid() {

        return cid;

    }



    public void setCid(String cid) {

        this.cid = cid;

    }



    public double getBalance() {

        return balance;

    }



    public void setBalance(double balance) {

        this.balance = balance;

    }



    public Customer getCustomer() {

        return customer;

    }



    public void setCustomer(Customer customer) {

        this.customer = customer;

    }



    public String getInfo(){

        return "cid="+cid+",balance="+balance;

    }

    //取钱方法

    public boolean withdraw(double monery){

        if(monery <= balance){

            balance = balance-monery;

            return true;

        }else {

            return false;

        }

    }

    //存钱方法

    public boolean save(double monery){

        try{

            balance = (double)monery+balance;

        }catch (Exception e){

            System.out.println("无法转换");

            return false;

        }

        return true;

    }



    public Account(String cid, double balance, Customer customer) {

        this.cid = cid;

        this.balance = balance;

        this.customer = customer;

    }

}

测试类:

public static void main(String[] args){

    Customer c1 = new Customer("小王","4409211","110","王庙村");

    Account a1 = new Account("123456",550000,c1);



}

2.方法的声明与调用

(1)声明一个圆柱体类型,

(2)声明属性:底边的半径,和高

(3)声明方法:

A:方法的功能:在方法中打印圆柱体的详细信息

       圆柱体的底边的半径是xxx,高是xxx,底面积是xxx,体积是xxx。

B:方法的功能:返回底面积

C:方法的功能:返回体积

D:方法的功能:为圆柱体的底边的半径,和高赋值

E:方法的功能:为圆柱体的底边的半径,和高赋值,并返回赋值的结果

       如果底边的半径或高为<=0,赋值失败,返回false,否则返回true

(4)并测试

       //声明圆柱体

class Cylinder{

         double radius;//底边半径

         double height;//高

        

         /*

         A:方法的功能:在方法中打印圆柱体的详细信息

         圆柱体的底边的半径是xxx,高是xxx,底面积是xxx,体积是xxx。

         */

         void printDetails(){

                  //double area = Math.PI * radius * radius;//底面积

                  //double volume = area * height;//体积

                 

                  //System.out.println("圆柱体的底边的半径是" + radius +" ,高是" + height + ",底面积是"+ area +",体积是"+volume +"。");

        

                  //调用本类的方法

                  System.out.println("圆柱体的底边的半径是" + radius +" ,高是" + height + ",底面积是"+ getArea() +",体积是"+getVolume() +"。")

         }

        

         //B:方法的功能:返回底面积

         double getArea(){

                  double area = Math.PI * radius * radius;//底面积

                  return area;

         }

        

         //C:方法的功能:返回体积

         double getVolume(){

                 

                  //double area = Math.PI * radius * radius;//底面积

                  //double volume = area * height;//体积

                  //return volume;

                 

                  double volume = getArea() * height;//体积

                  return volume;

         }

        

         //D:方法的功能:为圆柱体的底边的半径,和高赋值

         void setValue(double r, double h){

                  radius = r;

                  height = h;

         }

        

         /*

         E:方法的功能:为圆柱体的底边的半径,和高赋值,并返回赋值的结果

         如果底边的半径或高为<=0,赋值失败,返回false,否则返回true

         */

         boolean setRadiusAndHeight(double r, double h){

                  if(r<=0 || h<=0){

                          return false;

                  }

                  //radius = r;

                  //height = h;

                  setValue(r,h);

                  return true;

         }

        

}

class TestMethodExer{

         public static void main(String[] args){

                  //1、创建对象

                  Cylinder c = new Cylinder();

                  //c.radius = 2.0;

                  //c.height = 2;

                  c.setValue(2.0,2);

                 

                  c.printDetails();

                 

                  System.out.println("底面积: " + c.getArea());

                  System.out.println("体积: " + c.getVolume());

                 

                  boolean flag = c.setRadiusAndHeight(3.0, 5);

                  if(!flag){// 如果flag = false, !flag结果就是true,条件成立

                          System.out.println("赋值失败");

                  }else{

                          c.printDetails();

                  }

         }

}

3.

public class Test1 {

    public static boolean foo(char c) {

         System.out.print(c);

         return true;

    }

    public static void main(String[] args) {

         int i = 0;

         for (foo('A'); foo('B') && (i < 2); foo('C')) {

             i++;// 1 2

             foo('D');

         }

    }

}

答案:ABDCBDCB

面向对象性

面向对象三大特征的说明

答:面向对象有三大特点:封装、继承、多态。(如果要回答四个,可加上 抽象性 这一特点)

1.继承性:

继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。

2.封装性:

封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。

3. 多态性:

多态性是指允许不同类的对象对同一消息作出响应。多态性包括参数化多态性和包含多态性。多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。

4.抽象性:

抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是数据抽象。

作用域public,private,protected,以及默认不写时的区别

Java权限修饰符public、protected、(缺省)、private置于类的成员定义前, 用来限定对象对该类成员的访问权限。

四种访问权限修饰符

对于class的权限修饰只可以用public和default(缺省)。 Ø public类可以在任意地方被访问。 Ø default类只可以被同一个包内部的类访问。

关于参数传递

练习一

写出结果。

public class Test{

public static void leftshift(int i, int j){

        i+=j;

}

public static void main(String args[]){

int i = 4, j = 2;

leftshift(i, j);

System.out.println(i);

}

}

答案:4

和leftShift函数没关系。

练习二

写出结果。

public class Demo{

public static void main(String[] args){

int[] a=new int[1];

modify(a);

System.out.println(a[0]); //

}

public static void modify(int[] a){

a[0]++;

}

}

答案: 1

练习三

public class TestA {

int i ;

void change(int i){

i++;

System.out.println(i);

}

void change1(TestA t){

t.i++;

System.out.println(t.i);

}

public static void main(String[] args) {

TestA ta = new TestA();

System.out.println(ta.i); //

ta.change(ta.i);//

System.out.println(ta.i); //

ta.change1(ta);  //

System.out.println(ta.i);//

}

}

答:01011

练习四

写出结果

class Value{

    int i = 15;

}

class Test{

public static void main(String argv[]) {

Test t = new Test();

t.first();

}

public void first() {

int i = 5;

Value v = new Value();

v.i = 25;

second(v, i);

System.out.println(v.i);

}

public void second(Value v, int i) {

i = 0;

v.i = 20;

Value val = new Value();

v = val;

System.out.print(v.i + " " + i);

}

}

答:15 020

练习五

1. public class Test {

2. int x= 12;

3. public void method(int x) {

4. x+=x;

5. System.out.println(x);

6. }

7. }

Given:

34. Test t = new Test();

35. t.method(5);

What is the output from line 5 of the Test class?

A. 5

B. 10

C. 12

D. 17

E. 24

练习六

import java.util.Arrays;

public class PassValueExer2{

         public static void main(String[] args){

                  int[] array = {3,2,5,1,7};

                 

                  //调用sort方法,实现从大到小排序

                  //在此处补充代码

                  ....

                 

                  //显示结果

                  System.out.println("排序后的结果是:" + Arrays.toString(array));

         }

        

         //要求使用冒泡排序完成

         public void sort(//形参?){

                 

         }

}

答案:

/*

考点:

1、方法的参数传递机制

2、冒泡排序

*/

import java.util.Arrays;

public class PassValueExer2{

         public static void main(String[] args){

                  int[] array = {3,2,5,1,7};

                  PassValueExer2 exer = new PassValueExer2();

                  //调用sort方法,实现排序

                  exer.sort(array);//实参给形参的是地址,数组的首地址

                 

                  //遍历结果

                  System.out.println("排序后的结果是:" + Arrays.toString(array));

         }

        

         //功能:用冒泡排序,实现为数组排序,而且从大到小

         //形参的类型?我要把什么传过来

         //传递数组

         //接收的类型也肯定是数组,即形参接收实参,即形参的类型是int[]

         public void sort(int[] arr){

                  //冒泡排序

                  //在这里对谁排,对arr进行排序

                  for(int i=1; i<arr.length; i++){//多少轮

                          //每一轮,从左往后--》for(int j=0;...)

                          //要实现从大到小-->前面的元素比后面的元素小,就交换

                          //每一轮几次,

                          //假设,数组的长度为5

                          //第一轮:4次,i=1, j=0,1,2,3  j<4  j< arr.length-i

                          //第二轮:3次,i=2, j=0,1,2

                          for(int j=0; j<arr.length-i; j++){

                                   if(arr[j] < arr[j+1]){

                                            int temp = arr[j];

                                            arr[j] = arr[j+1];

                                            arr[j+1] = temp;

                                   }

                          }

                  }

         }

}

以下代码的执行结果是什么

    public static void main(String[] args) {

         int i = 0;

         change(i);

         i = i++;

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

    }

    public static void change(int i){

         i++;

    }

答案:i = 0

以下程序的运行结果:

    public static void main(String[] args) {

         String str = new String("world");

         char[] ch = new char[]{'h','e','l','l','o'};

         change(str,ch);

         System.out.println(str);

         System.out.println(String.valueOf(ch));

    }

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

         str = "change";

         arr[0] = 'a';

         arr[1] = 'b';

         arr[2] = 'c';

         arr[3] = 'd';

         arr[4] = 'e';

    }

答案:

world

abcde

以下代码的运行结果是?

public class Test {

    int a;

    int b;

    public void f(){

         a = 0;

         b = 0;

         int[] c = {0};

         g(b,c);

         System.out.println(a + " " + b + " " + c[0]);

    }

    public void g(int b, int[] c){

         a = 1;

         b = 1;

         c[0] = 1;

    }

    public static void main(String[] args) {

         Test t = new Test();

         t.f();

    }

}

答案:1 0 1

简答

当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?

答:是值传递。Java 编程语言只有值传递参数。当一个对象实例作为一个参数被传递到方法中时,参数的值就是对该对象的引用。对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的

补足compare函数内的代码,不许添加其他函数。

class Circle {

    private double radius;

    public Circle(double r) {

       radius = r;

    }

    public Circle compare(Circle cir) {

       // 程序代码

       /*

        * if(this.radius>cir.radius) return this; return cir;

        */

       // return (this.radius>cir.radius)?this: cir;

    }

}

class TC {

    public static void main(String[] args) {

       Circle cir1 = new Circle(1.0);

       Circle cir2 = new Circle(2.0);

       Circle cir;

       cir = cir1.compare(cir2);

       if (cir1 == cir)

           System.out.println("1的半径比较大");

       else

           System.out.println("2的半径比较大");

    }

}

  1. switch后面使用的表达式可以是哪些数据类型的。

答:switch表达式中必须是一下几种类型:byte、short、char、int、枚举(JDK1.5),在jdk1.7以后允许使用字符串(String)

2. 使用switch语句改写下列if语句:

       int a = 3;

       int x = 100;

       if(a==1)

              x+=5;

       else if(a==2)

              x+=10;

       else if(a==3)

              x+=16;

       else           

              x+=34;

答:

public static int a(){
   
int a = 3;
    int
x = 100;
    switch
(a*1){
       
case 1:
            x+=
5;
           
System.out.println(x);
            break;
        case
2:
            x+=
10;
           
System.out.println(x);
            break;
        case
3:
            x+=
16;
           
System.out.println(x);
            break;
        default
:
            x+=
34;
           
System.out.println(x);
   
}
   
return x;
}

3. 谈谈你对三元运算符、if-else和switch-case结构使用场景的理解

答:

表达式是等值判断的话 if switch都可以

表达式是区间判断的情况 if最好

Swich的应用场景:等值判断,等值的情况比较少的情况下

4. 如何从控制台获取String和int型的变量,并输出?使用代码实现

public static void a(){

    try{

        Scanner sc = new Scanner(System.in);

        //整形

        int num = sc.nextInt();

        //字符串

        String str = sc.next();

        System.out.println("num:"+num+",str:"+str);

    }

    catch (Exception e){

        System.out.println("对不起,赋值异常");

    }

}

5. 使用for循环遍历100以内的奇数,并计算所有的奇数的和并输出。

public static void a(){

    int sum=0;

    for (int i = 1;i <= 100;i+=2){

        System.out.println(i);

        sum+=i;

    }

    System.out.println(sum);

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值