第四个基础练习题

练习1:定义一个修饰权限为private的成员变量,并定义两个成员方法一个成员方法实现为次成员变量赋值,另一个成员方法获取这个成员变量的值,保证其他类集成该类时能获取该类的成员变量的值。

练习2:尝试编写一个矩形类,将长与宽作为矩形类的属性,在构造方法中将长、宽初始化,定义一个成员方法求此矩形的面积。

练习3:根据运行参数的个数决定循环打印变量i值的次数。

 1 /*
 2  * 
 3  * Excise by http://www.cnblogs.com/WhoMercy
 4  * 
 5  * */
 6 public class Exercise1 {
 7     private static String A;
 8     public static String fuzhi(){
 9         A="abcdefghijklmnopqrstuvwxyz";
10         return A;
11     }
12     public static String quzhi(String array){
13         String quzhi;
14         quzhi=array;
15         return quzhi;
16     }
17     public static void call(){
18         System.out.println(Exercise1.A);
19     }
20     public static void main(String[] args){
21         System.out.println("练习1:定义一个修饰权限为private的成员变量,并定义两个成员方法"+"\n"
22     +"一个成员方法实现为次成员变量赋值,另一个成员方法获取这个成员变量的值,"+"\n"+"保证其他类集成该类时能获取该类的成员变量的值。");
23         System.out.println();
24         fuzhi();
25         quzhi(A);
26         System.out.print("A的值为: ");
27         call();
28         System.out.print("quzhi的值为: ");
29         call();
30     }
31 
32 }
练习1
 1 /*
 2  * 
 3  * Excise by http://www.cnblogs.com/WhoMercy
 4  * 
 5  * */
 6 public class Exercise2 {
 7     public static int length;
 8     private static int width;
 9     public static void main(String[] args){
10         System.out.println("练习2:尝试编写一个矩形类,将长与宽作为矩形类的属性,"
11     +"\n"+"在构造方法中将长、宽初始化,定义一个成员方法求此矩形的面积。");
12         Exercise2 m=new Exercise2(7,3);
13         System.out.println("面积是: "+m.acreage());
14     }
15     public Exercise2(int length,int width){
16         this.length=length;
17         this.width=width;
18         }
19     public int acreage(){
20         int acreage = length*width;
21         return acreage;
22     }
23 
24 }
练习2
 1 /*
 2  * 
 3  * Excise by http://www.cnblogs.com/WhoMercy
 4  * 
 5  * */
 6 public class Exercise3 {
 7     public static int a;
 8     public static int b;
 9     public static void main(String[] args){
10         System.out.println("练习3:根据运行参数的个数决定循环打印变量i值的次数。");
11         Exercise3 a=new Exercise3(3,4);
12         a.call();
13     }
14     public static int number(){
15         int number=a*b;
16         return number;
17     }
18     public Exercise3(int a,int b){
19         this.a=a;
20         this.b=b;
21     }
22     public void call(){
23         System.out.println("a的值为: "+this.a+"\n"+"b的值为: "+this.b);
24         System.out.println("循环打印次数(number) = a*b.");
25         for(int i=1;i<=Exercise3.number();i++){
26             System.out.println("这是第 "+"\t"+i+"\t"+"次打印");
27         }
28     }
29 
30 }
练习3

 

转载于:https://www.cnblogs.com/WhoMercy/p/3446498.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值