类与对象练习一

一:编写类A01,定义方法max,实现求某个double数组的最大值,并返回

public class HomeWork01{
	public static void main(String[] args) {
		double [] arr = {56.56,45.56,4545.55,45.66};
		A01 t = new A01();
		System.out.println("最大值为"+t.max(arr));
	}
}
class A01{
	public double max(double[] arr){
		double max = 0;
		for (int a = 0;a<arr.length ;a++ ) {
		 	if (arr[a]>max) {
		 		max = arr[a];
		 	}
		 } 
		 return max;
	}
}

二:编写类A02,定义方法find,实现查找字符串数组中的某个值,并返回下标,如果找不到返回-1

public class HomeWork01{
	public static void main(String[] args) {
		
		A02 t = new A02();
		String [] arr ={"张三","李四","王五"};
		System.out.println(t.find(arr,"王五"));
		
	}
}
class A02{
	public int find(String[] arr,String a){


		for (int i = 0;i<arr.length ;i++ ) {
			if (a.equals(arr[i])) {
				return i;
			}
		}

		return -1 ;//如果没有for循环没有找到符合的直接返回-1
	}
}

三:编写类Book,定义构造器方法,实现更改某本书的价格,具体:如果价格>150则更改为150,如果价格>100,更改为100,其他不变。

public class HomeWork01{
	public static void main(String[] args) {
		Book t = new Book("《演员的自我修养》",256);
		Book t1 = new Book("《java核心技术卷1》",120);
	}
}
class Book{
	String name;
	double price;
	Book(String name,double price){
		this.name = name;
		if (price>100 && price<150) {
			this.price = 100;
		}else if (price > 150 ) {
			this.price = 150;
		}
		System.out.println(this.name+"\t"+this.price);
	}
}

四:编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样

public class HomeWork01{
	public static void main(String[] args) {
		A03 t = new A03();
		int []arr1 = {5,4,5,4,8};
		int []newArr =t.copyArr(arr1);
		for (int a : newArr ) {
			System.out.print(a);
		}
	}
}
class A03{
	public int [] copyArr(int [] arr){
		int [] arr2=new int[arr.length];
		for (int i=0;i<arr.length ;i++ ) {
			arr2[i]=arr[i];
		}
		return arr2;
	}
	
}

五:定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示园面积的方法

周长公式:2Πr
面积公式:Πrr

public class HomeWork01{
	public static void main(String[] args) {
		Circle t = new Circle(50);
		System.out.println("周长为"+t.perimeter()+"\n面积为"+t.area());
	}
}
class Circle{
	double radius;
	Circle(double radius){
		this.radius = radius;
	}
	public double perimeter(){
		return 2*Math.PI*radius;
	}
	public double area(){
		return Math.PI*radius*radius;
	}
	
}

六:以下代码会输出什么

public class HomeWork01{
	public static void main(String[] args) {
		new T().count1();//匿名对象,只能使用一次
		T t = new T();
		t.count2();
		t.count2();
		
		
	}
}
class T{
	int num = 9;
	public void count1(){
		num = 10;
		System.out.println(num);
	}
	public void count2(){
		System.out.println(num++);//++在后,先输出后自增
	}
	
}

10
9
10
注意点1:注意new T().count1和t.count1是不同的两个对象,地址也不同所以变量num会重新变成9
注意点2:++在后,先输出后自增

七:在一个测试类中有一条语句:

System.outprintln(method(method(10.1,20.3),100));写出一个可以编译的method方法

public double method(double a,double b){
			return a+b;
		}

八:创建一个A05类,属性有(名字,年龄,性别,职位,薪水)。提供三个构造器可以初始化(1)(名字,性别,年龄,职位,薪水)(2)(名字,性别,年龄)(3)(职位,薪水),要求复用构造器

class T{
	String name;
	int age;
	char gender;
	String post;
	double salary;
	T(String post,double salary){
		this.post = post;
		this.salary = salary;
	}
	T(String name,char gender,int age){
		this.name = name;
		this.age = age;
		this.gender = gender;
	}
	T(String name,char gender,int age,String post,double salary){
		this.(name,gender,age);
		this.post = post;
		this.salary = salary;

	}

}

练习点:构造器调用另一个构造器this(形参);。注意用this调用另一个构造器只能在第一行

九:将对象作为参数传递给方法

  • 题目要求定义一个Circle类,包含一个double型的radius属性代表圆的半径,findArea()方法返回圆的面积
  • 定义一个PassObject类在类中定义一个方法printAreas(),该方法的定义如下:public voidprintAreas(Circle c,int times)在此方法中打印输出1到times之间的每个整数半径值,以及对应的面积 例如:times是5 则输出半径1-2-3-4-5,以及对应的圆面积
  • 在main方法中调用printAreas()方法,调用完毕后输出当前半径值。
public class HomeWork01{
	public static void main(String[] args) {
		Circle c = new Circle();
		PassObject op = new PassObject();
		op.printAreas(c,5);
	
	}
}
class Circle{
	double radius;

	public double findAreas(){
		return Math.PI*radius*radius;
	}
	public void setRadius(double radius){
		this.radius = radius;
	}

}
class PassObject{
	public void printAreas(Circle c ,int times){
		System.out.println("radius\tAreas");
		for (int i = 1;i<=times ;i++ ) {
			c.setRadius(i);
			System.out.println(c.radius+"\t"+c.findAreas());
		}
	}
}

难点:未接触知识点Set方法 封装会接触到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值