java快速复习07-面向对象基础题目

java快速复习07-面向对象基础题目

01.编写AMax,定义方法max,实现求某个double数组最大值,并返回。
public class Homework01 {
	public static void main(String[] args) {
		AMax aMax = new AMax();
		double[] arr = {1.2, 2.4, 1.1, 5.1, 3.2};
		System.out.println("arr数组的最大值是" + aMax.max(arr));
	}
}
class AMax {
	/*
	思路:
	1. 方法名max
	2. 形参(double[])
	3. 返回值 double
	 */
	public double max(double[] arr) {
		double max = arr[0];
		//假定第一个元素就是最大值
		for (int i = 1; i < arr.length; i++) {
			if (max < arr[i]) {
				max = arr[i];
			}
		}
		return max;
	}
}
02.编写AFind,定义方法find,实现查找某字符串是否在字符串数组中,并返回索引,如果找不到返回-1。
public class Homework02 {
	public static void main(String[] args) {
		AFind aFind = new AFind();
		String[] strs = {"jack", "tom", "marry", "milan"};
		int index = aFind.find("tom", strs);
		System.out.println(index);
	}
}
class AFind {
	/*
	思路:
	1. 方法名find
	2. 返回值int
	3. 形参 (String, String[])
	 */
	public int find(String findStr, String[] strs) {
		for (int i = 0; i < strs.length; i++) {
			if (findStr.equals(strs[i])) {
				return i;
			}
		}
		//如果没有就返回-1
		return -1;
	}
}
03. 编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150;如果价格>100,更改为100,否则不变。

备注:考察对this的使用

public class Homework03 {
	public static void main(String[] args) {
		Book book = new Book("天龙八部", 120);
		book.info();
		book.undatePrice();
		book.info();
	}
}
class Book {
	/*
	思路:
	1.属性:price, name
	2.方法名:updatePrice
	3.形参可以是空
	4.返回值不需要
	5.提供一个构造器
	 */
	String name;
	double price;
	public Book(String name, double price) {
		this.name = name;
		this.price = price;
	}
	public void undatePrice() {
		if (this.price > 150) {
			this.price = 150;
		} else if (this.price > 100) {
			this.price = 100;
		}
	}
	public void info() {
		System.out.println("书名:" + this.name + " 价格:" + this.price);
	}
}
04. 编写类Acopy,实现数组的复制功能copyArr,输入就数组,返回一个新数组,元素和旧数组一样。
public class Homework04 {
	public static void main(String[] args) {
		int[] oldArr = {3, 5, 6, 1, 3};
		Acopy aCopy = new Acopy();
		int[] newArr = aCopy.copyArr(oldArr);
		for (int i = 0; i < newArr.length; i++) {
			System.out.print(newArr[i] + "\t");
		}
	}
}
class Acopy {
	public int[] copyArr(int[] oldArr) {
		int[] newArr = new int[oldArr.length];
		for (int i = 0; i < oldArr.length; i++) {
			newArr[i] = oldArr[i];
		}

		return newArr;
	}
}
05. 定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法。
public class Homework05 {
	public static void main(String[] args) {
		Cirlce circle = new Cirlce(3);
		System.out.println("面积:" + circle.area());
		System.out.println("周长:" + circle.len());
	}
}

class Cirlce {
	double radius;

	public Cirlce(double radius) {
		this.radius = radius;
	}
	public double area() {
		return Math.PI * radius * radius;
	}
	public double len() {
		return 2 * Math.PI *radius;
	}
}
06.编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,定义四个方法实现求和、差、乘、商(要求除数为零的话,要提示)并创建两个对象分别测试。
public class Homework06 {
	public static void main(String[] args) {
		Cale cale = new Cale(4, 0);
		System.out.println("和:" + cale.sum());
		System.out.println("差 " + cale.minus());
		System.out.println("乘 " + cale.mul());
		Double divRes = cale.div();
		if (divRes != null) {
			System.out.println("除 " + divRes);
		}
	}
}

class Cale {
	double num1;
	double num2;
	public Cale(double num1, double num2) {
		this.num1 = num1;
		this.num2 = num2;
	}
	public double sum() {
		return num1 + num2;
	}
	public double minus() {
		return num1 - num2;
	}
	public double mul() {
		return num1 * num2;
	}
	public Double div() {
		if (num2 == 0) {
			System.out.println("除数不能为零");
			return null;
		}
		return num1 / num2;
	}
}
07.设计一个Dog类,有名字、颜色和年龄属性,定义输出方法show()显示其信息。并创建对象,进行测试(提示this.属性)。
public class Homework07 {
	public static void main(String[] args) {
		Dog dog = new Dog("jack", "pink", 4);
		dog.show();
	}
}
class Dog {
	String name;
	String color;
	int age;

	public Dog(String name, String color, int age) {
		this.name = name;
		this.color = color;
		this.age = age;
	}
	public void show() {
		System.out.println("名字:" + name + " 顏色:" + color +" 年齡:" + age);
	}
}
08. 给定代码如下,编译运行后,输出的结果是?
public class Test {
	int count = 9;
	public void count1() {
		count = 10;
		System.out.println(count);
	}
	public void count2() {
		System.out.println(count++);
	}
	public static void mian(String[] args) {
		new Test().count1();
		Test t1 = new Test();
		t1.count2();
		t1.count2();
	}
}

结果:10 9 10

09. 定义Music类,里面有音乐名name,音乐时长times属性,并有播放play功能和返回属性信息的功能方法getInfo。
public class Homework09 {
	public static void main(String[] args) {
		Music music = new Music("小螺号", 200);
		music.play();
		System.out.println(music.getInfo());
	}
}
class Music {
	String name;
	int times;
	public Music(String name, int times) {
		this.name = name;
		this.times = times;
	}
	public void play() {
		System.out.println("音乐 " + name + " 正在播放中...时长为" + times + "秒");
	}
	public String getInfo() {
		return "音乐 " + name + " 播放时间为" + times;
	}
}
10. 阅读下面代码,写出显示的结果。
class Demo {
	int i = 100;
	public void m() {
		int j = i++;
		System.out.println(i);
		System.out.println(j);
	}
}
class Test {
	public static void main(String[] args) {
		Demo d1 = new Demo();
		Demo d2 = d1;
		d2.m();
		System.out.println(d1.i);
		System.out.println(d2.i);
	}
}

结果是:101 100 101 101

11.在测试方法中,调用method方法,代码如下,编译正确,试写出method方法的定义形式,调用语句System.out.println(method(method(10.0, 20,0), 100));

这个题目主要考察,从调用形式反推定义形式

public double method(double d1, double d2) {.....}
12. 创建一个Employee类,属性有(名字,性别,年龄,职位,薪水),提供3个构造器方法,可以初始化 1)(名字,性别,年龄,职位,薪水)2)名字 性别 年龄 3)职位 薪水,要求充分利用构造器
public class Homework12 {
	public static void main(String[] args) {
		
	}
}
class Employee {
	String name;
	char gender;
	int age;
	String job;
	double sal;
	//职位 薪水
	public Employee(String name, double sal) {
		this.job = job;
		this.sal = sal;
	}
	//名字 性别 年龄
	public Employee(String name, char gender, int age) {
		this.name = name;
		this.gender = gender;
		this.age = age;
	}
	//五个属性同时在
	public Employee(String name,double sal, char gender, int age) {
		this(name, gender,age);
		this.job = job;
		this.sal = sal;
	}
}
13.将对象作为参数传递给方法。

题目要求:

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

class Circle {
	double radius;
	public Circle() {

	}
	public Circle(double radius) {
		this.radius = radius;
	}
	public double findArea() {
		return Math.PI * radius * radius;
	}
	//添加一个修改的radius方法
	public void setRadius(double radius) {
		this.radius = radius;
	}
}
class PassObject {
	public void printAreas(Circle c, int times) {
		for (int i = 1; i <= times; i++) {
			c.setRadius(i);
			System.out.println(i + "\t" + c.findArea());
		}
	}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值