Java 面向对象练习与对象数组练习 -31天 学习笔记

练习1

1.编写程序,声明一个method方法,在方法中打印一个10✖️8的⭐型矩形,在main方法中调用该方法。
2.修改上一个程序,在method方法中,除打印一个10✖️8的⭐型矩形外,再计算该矩形的面积

并将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。

3.修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m* n的*型矩形,

4.并计算该矩形的面积,将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。

package com.xin.OOP;
/*

*/
public class Ooptext
{
	public static void main (String[] args){
			//因为method方法没有加static 所以我们要重新new个对象
			Ooptext o1  = new Ooptext();
			//方法1 o1.method();
			
//		 方法2  int arae	= o1.method();
//				System.out.println("面积为"+arae);
//			 或者System.out.println("面积为"+o1.method());
			//方法3
			   int print = o1.method(5,5);
			  System.out.println("面积为"+print);
			}
//			方法1
//			public void method(){
//					for(int i = 0;i < 8;i++){
//							for(int j = 0;j < 10;j++){
//									System.out.print("* ");
//							}
//					   System.out.println();
//						}
//			}
			
			//方法2  计算矩阵面积
//	   	public int method(){
//			
//	   		for(int i = 0;i < 8;i++){
//				
//			        for(int j = 0;j < 10;j++){
//					
//			         System.out.print("* ");
//		     	}
//					   System.out.println();
//			
//			}
//		  return 10*8;
//			}
			
			//方法3   返回m*n列的矩阵
			
			public int method(int m,int n){
					for(int i = 0;i < n;i++){
							for(int j = 0;j < m;j++){
									Sys

练习2

对象数组题目:

定义类Student,包含三个属性:学号number(int),年级state(int), 成绩

score(int)。创建20个学生对象, 学号为1到20,年级和成绩都由随机数确定。问题一:打印出3年级(state值为3)的学生信息。

问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息

提示:

1)生成随机数: Math.random(), 返回值类型double;

2)四舍五入取整: Math.round(double d),返回值类型long。

package com.xin.OOP;

public class Teacher
{
		public static void main (String[] args){
				//创建20个学生对象数组
				Studen1t[] stu = new Studen1t[20];
				
				
				//给20个学生赋值
				for(int i = 0;i < stu.length; i++){
						//给每一个位置new 一个对象
						//第一步只是定义了数组,数组元素本身还没实例化,也不指向具体对象
						//第一行是说定义一个长度为20,元素类型为stu的数组,只是数组的空间被开辟出来了里面本来要放地址,
						//但是具体的stu空间还没开辟,所以是空指针
					 stu[i] =	new Studen1t(); //刚开始new的是数组,现在new的是对象
						//赋值学号1-20
						stu[i].number = i+1;
						//赋值且随机年级1-6
						stu[i].state = (int)(Math.random()* (6-1+1)+1);
						//随机赋值成绩0-100
						stu[i].score = (int)(Math.random() *100-0+1);
				}
				//打印学生对象数组
				Studen1t s1 = new Studen1t();
				s1.print(stu);
				System.out.println("=============");
				
				//查找3年级学生成绩
				s1.searchState(stu,3);
				System.out.println("============");
				
				//给学生排序且打印
				s1.shor(stu);
				s1.print(stu);
				//
				s1.a(stu);
		}
}



package com.xin.OOP;

public class Studen1t
{
		int number;
		int state;
		int score;
		//显示学生信息的方法
		public  String info(){
				return "学号"+number+" 年级"+state+" 成绩"+score;
		}
		
			//打印对象数组
		public void print(Studen1t[] arr){
				for(int i = 0;i < arr.length;i++){
						System.out.println(arr[i].info());
				}
		}
		//查找对象数组中的学生年级信息
		public void searchState(Studen1t[] arr,int a){
				for(int i = 0;i<arr.length;i++){
						if(arr[i].state == a){
								System.out.println(arr[i].info());
						}
				}
		}
		
	//冒泡排序,根据成绩交换对象数组的值
	public void shor(Studen1t[] arr){
			for(int i = 0;i < arr.length-1;i++){
					for(int j = 0;j < arr.length-1-i;j++){
							//比较成绩
							if(arr[j].score > arr[j+1].score){
							//交换对象数组中的值
				   			Studen1t temp = arr[j];
									       	arr[j] = arr[j+1];
								     		arr[j+1] = temp;
															
							}
					}
			}
	}
	
	public Studen1t[] a(Studen1t[] stu){
			
			for(int i = 0;i < stu.length; i++){
						//给每一个位置new 一个对象
						//第一步只是定义了数组,数

练习3

*要求:
(1 )创建Person类的对象,设置该对象的name、age和sex属性,
调用study方法,输出字符串studying”,
调用showAge( )方法显示age值,

*调用addAge( )方法给对象的age属性值增加2岁。

(2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系。
*/

package com.xin.OOP;
/*
*要求:
(1 )创建Person类的对象,设置该对象的name、age和sex属性,
调用study方法,输出字符串studying”,
调用showAge( )方法显示age值,

*调用addAge( )方法给对象的age属性值增加2岁。

(2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系。
*/

public class Person
{
		String name ;
		int age;
		/*
		sex = 1为男性 ,sex =2为女性
		**/
		
		int sex;
		
		public void study(){
				System.out.println(name+"在studying");
		}
		
		public void showAge(){
				System.out.println("age为"+age);
		}
		
		public int addAge(int i){
				age += i;
				return age;
				
				/*
					Person p1 =	new Person();
					
		 p1.name = "李华";
		
					    p1.age = 18;
		
									p1.sex = 1;
			
			
		
									p1.study();
			
									p1.showAge();
			
									int age1 = p1.addAge(2);
			
									//调用的main方法中的成员变量
		
									System.out.println(age1);
	
									//调用的堆中的属性
		
									p1.showAge();
   
									//new 个person
			
									Person p2 = new Person();
			
									p2.showAge();//0
				*/
		}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值