return关键字,显示学生信息的方法,对象数组题目,类和对象的使用

在这里插入图片描述

package atwanyi.java;
/*
 * 一、设计类,其实就是设计类的成员
 * 
 * 属性= 成员变量 = field = 域、字段
 * 方法= 成员方法 = 函数 =method
 * 
 * 创建类的对象 = 类的实例化 = 实例化类
 * 
 *二:类和对象的使用(面对对象思想落地的实现):
 *1.创建类,设计类的成员
 *2.创建类的对象
 *3.通过“对象.属性”或“对象.方法”调用对象的结构
 *
 *三:如果创建了一个类的多个对象,则每个对象都独立的拥有一套类的属性。(非static)
*意味着:如果我们修改一个对象的属性a,则不影响另外一个对	象	属性a的值。
 *
*四:对象的内存解析
 */
//测试类
public class PersonTest {
public static void main(String[] args) {
	//2.创建Person类的对象
	Person p1 = new Person();
	//Scanner scanner = new Scanner(System.in);
	
	//调用对象的结构:属性、方法
	//调用属性:“对象、属性”
	p1.name = "Tom";
	p1.isMale = true;
	System.out.println(p1.name);
	
	//调用方法:“对象.方法”
	p1.eat();
	p1.sleep();
	p1.talk("Chinese");
	
	//**********************
	Person p2 = new Person();
	System.out.println(p2.name);//null
	System.out.println(p2.isMale);
	//**********************
	//将p1变量保存的对象地址值赋给p3,导致p1和p3指向了堆空间中的同一个对象实体。
	Person p3 = p1;
	System.out.println(p3.name);//Tom
	
	p3.age = 10;
	System.out.println(p1.age);//10
}
}
class Person{
//属性
String name;
int age = 1;
boolean isMale;

//方法
public void eat() {
	System.out.println("人可以吃饭");
}
public void sleep() {
	System.out.println("人可以睡觉");
}
public void talk(String language) {
	System.out.println("人可以说话,使用的是:" + language);
}
}

在这里插入图片描述

package atwanyi.java;
/*
 * 类中方法的声明和使用
 * 
 * 方法:描述类应该具有的功能。
 * 比如:Math类、sqrt()\random()\...
 * 		Scanner类:nextXxx()...
 * 		Arrays类:sort()\binarySearch()\toString()\equals()\...
 * 1.举例:
 * public void eat() {}
 * public void sleep(int hour) {}
 * public String getName() {}
 * public String getNation(){}
 * 
* 2.方法的声明:权限修饰符 返回值类型 方法名(形参列表){
 * 			方法体
 * }
 */
public class CustomerTest {

}
class Customer{

//属性
String name;
int age;
boolean isMale;

//方法
public void eat() {
	System.out.println("客户吃饭");
}

public void sleep(int hour) {
	System.out.println("休息了" + hour + "个小时");
}

public String getName() {
	return name;
}

public String getNation(String nation) {
	String info = "我的国籍是:" + nation;
	return info;
}
}

return关键字的使用:
1.使用范围:使用在方法体中
2.作用:
①结束方法
②针对于有返回值类型的方法,使用“return 数据”方法返回所要的数据。
3.注意点:return关键字后面不可以声明执行语句。

5#
方法使用中,可以调用当前类的属性或方法
特殊的:方法A中又调用了方法A:递归方法。
方法中,不可以定义方法。
6#

package atwanyi.exer;

public class Person {
String name;
int age;
/*
 * sex:1表明是男性
 * sex:0表明是女性
 */
int sex;

public void study() {
	System.out.println("studying");
}

public void showAge() {
	System.out.println("age:" + age);
}

public int addAge(int i) {
	age += i;
	return age;
}
}
2##package atwanyi.exer;

public class PersonTest {
public static void main(String[] args) {
	Person p1 = new Person();
	
	p1.name = "Tom";
	p1.age = 18;
	p1.sex = 1;

	p1.study();
	
	p1.showAge();
	
	int newAge = p1.addAge(2);
	System.out.println(p1.name + "的新年龄为:" + newAge);
	
	System.out.println(p1.age);//20
	
	//***********************
	Person p2 = new Person();
	p2.showAge();//0
	p2.addAge(10);
	p2.showAge();//10
	
	p1.showAge();
}

}
package atwanyi.exer;
/*
 * 2.利用面向对象的编程方法,设计类Circle计算圆的面积。
 */
//测试类
public class CircleTest {
public static void main(String[] args) {
	
	Circle c1 = new Circle();
	
	c1.radius = 2.1;
	
	//对应方式一:
//		double area = c1.findArea();
//		System.out.println(area);
	
	//对应方式二:
	c1.findArea();
	
	//错误的调用
//		double area = c1.findArea(3.4);
//		System.out.println(area);
}
}

//圆
class Circle{

//属性
double radius;

//	//求圆的面积
//	public double findArea() {
//		double area = Math.PI * radius * radius;
//		return area;
//	}
//方式二:
public void findArea() {
	double area = Math.PI * radius * radius;
	System.out.println("面积为:" + area);
}

//错误情况:
public double findArea(double r) {
	double area = 3.14 * r * r;
	return area;
}
}

#7.

package atwanyi.exer;
/*
 * 3.1编写程序,声明一个method方法,在方法中打印一个10*8的*型矩形,
 * 在main方法中调用该方法。
 * 3.2修改上一个程序,在method方法中,除打印一个10*8的*型矩形外,再
 * 计算该矩形的面积。并将其作为方法返回值。在main方法中调用该方法,接
 * 受返回的面积值并打印。
 * 
 * 3.3修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m*n
 * 的*矩形,并计算该矩形的面积,将其作为方法返回值。在main方法中调用该
 * 方法,接受返回的面积值并打印。
 * 
 */
public class Exer3Test {
public static void main(String[] args) {
	Exer3Test test = new Exer3Test();
	
	//3.1测试
//		test.method();
	
	//3.2测试
	//方式一:
//		int area = test.method();
//		System.out.println("面积为:" + area);
	
	//方式二:
//		System.out.println(test.method());
	
	//3.3测试
	int area = test.method(12, 10);
	System.out.println("面积为:" + area);
}
//3.1
//	public void method() {
//		for(int i = 0;i < 10;i++) {
//			for(int j = 0;j < 8;j++) {
//				System.out.print("* ");
//			}
//			System.out.println();
//		}
//	}
	//3.2
//	public int method() {
//		for(int i = 0;i < 10;i++) {
// 			for(int j = 0;j < 8;j++) {
// 				System.out.print("* ");
// 			}
// 			System.out.println();
// 		}
//		return 10 * 8;
//	}
	//3.3
	public int method(int m,int n) {
		for(int i = 0;i < m;i++) {
		for(int j = 0;j < n;j++) {
			System.out.print("* ");
		}
		System.out.println();
	}
	return m * n;
}
}

#8.

package atwanyi.exer;
/*
 * 4.对象数组题目:
 * 定义类Student,包含三个属性,学号number(int),年级state(int),成绩score(int)。
 * 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
 * 问题一:打印出3年级(state值为3)的学生信息。
 * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生的信息
 * 
 * 提示:
 * 1)生成随机数,Math.random() ,返回值类型double;
 * 2)四舍五入取整,Math.round(double d),返回值类型long。
 *  
 * 此代码是对StudentTest.java的改进:将操作数组的功能封装到方法中。
 */
public class StudentTest1 {
	public static void main(String[] args) {
		
//		Student s1 = new Student();
//		Student s1 = new Student();
//		Student s1 = new Student();
//		Student s1 = new Student();
//		Student s1 = new Student();
		
		//声明Student类型的数组
		Student1[] stus = new Student1[20];
		for(int i = 0;i < stus.length;i++) {
			stus[i] = new Student1();
			//给Student1对象的属性赋值
			stus[i].number = i + 1;
			//年级:[1,6]
			stus[i].state = (int)(Math.random() * (6-1+1)+1);
			//成绩:[0,100]
			stus[i].score = (int)(Math.random() * (100-0+1));
		}
		
		StudentTest1 test = new StudentTest1();
		
		//遍历学生数组
		test.print(stus);
		
		System.out.println("*************");
	
		//问题一:打印出3年级(state值为3)的学习信息。
		test.searchState(stus, 3);
		
		System.out.println("*************");
		
		//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
		test.sort(stus);
		
		//遍历学生数组
		test.print(stus);
	 }
	
	//遍历Student1[]数组的操作
	public void print(Student1[] stus) {
		for(int i = 0;i < stus.length;i++) {			
			System.out.println(stus[i].info());
		}
	}
	/**
	 * 
	 * @Description 查找Student数组中指定年级的学生信息
	 * @author aichiroudedabaitu
	 * @date 2019年5月25日下午12:14:52
	 * @param stus 要查找的数组
	 * @param state 要找的年级
	 */
	public void searchState(Student1[] stus,int state) {
				for(int i = 0;i < stus.length;i++) {
				if(stus[i].state == state) {
					System.out.println(stus[i].info());
				}
			}
		}

/**
 * 
 * @Description  给Student1数组排序
 * @author aichiroudedabaitu
 * @date 2019年5月25日下午12:21:37
 * @param stus
 */
public void sort(Student1[] stus) {
	for(int i =0;i < stus.length -1;i++) {
		for(int j = 0;j < stus.length - 1 -i;j++) {
			if(stus[j].score > stus[j+1].score) {
				//如果需要换序,交换的是数组的元素:Student1对象!
				Student1 temp = stus[j];
				stus[j] = stus[j+1];
				stus[j+1] = temp;
			}
		}
	}
}
}
class Student1{
int number;//学号
int state;//年级
int score;//成绩

//显示学生信息的方法
public String info() {
	return "学号: " + number + ",年级: " + state + ",成绩: " + score;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值