Java编程练习题:面向对象练习

目录

1. (LeetCode 第169题)给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数大于 n/2 的元素。

2. 综合案例(用户博客管理系统)

3. (抽象类)定义一个抽象的"Role"类 有姓名 年龄 性别等成员变量

4. (抽象类)品尝饮料


1. (LeetCode 第169题)给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数大于 n/2 的元素。

例如:[1,1,1,1,2,3,3],多数元素为1

方法1:排序数组,找最中间的数

如果将数组 nums 中的所有元素按照单调递增或单调递减的顺序排序,那么下标为nums.lenght/2的元素(下标从 0 开始)即最中间的数一定是众数。

方法2:抵消法

循环遍历数组,下一个数相同时计数加1,下一个数不同计数就减去1,当为count==0时,把num[i]赋值给众数,直到最后,得到的数就是众数

import java.util.Arrays;

public class FindRepeatNum {
	public static void main(String[] args) {
//		int[] nums = { 1, 1, 1, 1, 2, 3, 3 };
		int[] nums = { 2, 2, 1, 1, 1, 2, 2 };
//		int[] nums = { 1, 3, 1, 1, 4, 1, 1, 5, 1, 1, 6, 2, 2 };
		int result = findRepeatMax(nums);
		System.out.println("出现次数大于" + nums.length / 2 + "的元素为" + result);
		int result1 = majorElements(nums);
		System.out.println("出现次数大于" + nums.length / 2 + "的元素为" + result1);
	}

	/*
	 * 给定一个大小为 n 的数组 nums,返回其中的多数元素。
	 * 多数元素是指在数组中出现次数大于 n/2 的元素。
	 */
	private static int findRepeatMax(int[] nums) {
		Arrays.sort(nums);
		return nums[nums.length / 2];
	}

	// nums[i] 	1 2 2 1 1 1 1 2 3
	// major 	1 / 2 / 1 1 1 1 1
	// count 	1 0 1 0 1 2 3 2 1

	// nums[i] 	1 2 2 1 1 1 1 2 3
	// major 	1 2 2 2 1 1 1 1 1
	// 			  0     0
	// count. 	1 1 2 1 1 2 3 2 1
	private static int majorElements(int[] nums) {
		int majority = nums[0];
		int count = 1;
		for (int i = 1; i < nums.length; i++) {
			if (count == 0) {
				count++;
				majority = nums[i];
			} else if (nums[i] == majority) {
				count++;
			} else if (nums[i] != majority) {
				count--;
			}
//			if (count == 0) {
//				count++;
//				majority = nums[i];
//			}
		}
		return majority;
	}
}

结果:

Java编程练习题:面向对象练习_第1张图片

2. 综合案例(用户博客管理系统)

Bolg类(抽象类):

package com.openlab.day13.exer1;

public abstract class Blog {
	final static int Board = 1; // 板块
	final static int Article = 2; // 文章
	final static int Author = 3; // 作者
	final static int Comment = 4; // 作者

	// 抽象方法:showPage()页面展示
	public abstract void showPage();
}

Board类:

package com.openlab.day13.exer1;

//import java.util.Arrays;

public class Board extends Blog {
	private int id; // 板块id
	private String name; // 板块名称
	private String intro; // 简介或描述

//	开发中尽量使用单项关联,双向关联会造成冗余
//	private Article[] articles;

	@Override
	public String toString() {
		return name;
	}

	public Board() {
		super();
	}

	public Board(int id, String name, String intro) {
		super();
		this.id = id;
		this.name = name;
		this.intro = intro;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	@Override
	public void showPage() {
		// TODO Auto-generated method stub
		System.out.println("********************模块详情********************");
		System.out.println("  作者编号:");
		System.out.println("\t" + this.getId());
		System.out.println("  作者姓名");
		System.out.println("\t" + this.getName());
		System.out.println("  作者简介:");
		System.out.println("\t" + this.getIntro());
		System.out.println("************************************************");
		System.out.println();
	}

}

Author类:

package com.openlab.day13.exer1;

//import java.util.Arrays;

public class Author extends Blog {
	private int id; // 作者id
	private String name; // 姓名
	private String gender; // 性别
	private int age; // 年龄
	private String intro = "作者很懒,什么业没留下~"; // 简介或描述
//	private Article[] articles;

	@Override
	public String toString() {
		return name;
	}

	public Author(int id, String name, String gender, int age, String intro) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.intro = intro;
	}

	public Author() {
		super();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	@Override
	public void showPage() {
		// 作者详情页
		System.out.println("********************作者详情********************");
		System.out.println("  作者编号:");
		System.out.println("\t" + this.getId());
		System.out.println("  作者姓名");
		System.out.println("\t" + this.getName());
		System.out.println("  作者性别:");
		System.out.println("\t" + this.getGender());
		System.out.println("  作者年龄:");
		System.out.println("\t" + this.getAge());
		System.out.println("  作者简介:");
		System.out.println("\t" + this.getIntro());
		System.out.println("************************************************");
		System.out.println();
	}
}

Article类:

package com.openlab.day13.exer1;

import java.util.Date;

//import java.util.Arrays;

public class Article extends Blog {
	private int id; // 文章id
	private String tittle; // 标题
	private String content; // 内容
	private Author Author; // 作者
	private Board board; // 所属板块
	private Date publishTime; // 发布时间
	private Date modifyTime; // 修改时间
	@Override
	public String toString() {
		return tittle;
	}
	
	public Article(int id, String tittle, String content, Author Author, Board board, Date publishTime,
			Date modifyTime) {
		super();
		this.id = id;
		this.tittle = tittle;
		this.content = content;
		this.Author = Author;
		this.board = board;
		this.publishTime = publishTime;
		this.modifyTime = modifyTime;
	}
	
	public Article() {
		super();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTittle() {
		return tittle;
	}

	public void setTittle(String tittle) {
		this.tittle = tittle;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Author getAuthor() {
		return Author;
	}

	public void setAuthor(Author Author) {
		this.Author = Author;
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public Date getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@Override
	public void showPage() {
		// 文章详情页
		System.out.println("********************文章详情********************");
		System.out.println("  文章编号:");
		System.out.println("\t" + this.getId());
		System.out.println("  文章标题:");
		System.out.println("\t" + this.getTittle());
		System.out.println("  文章作者:");
		System.out.println("\t" + this.getAuthor());
		System.out.println("  发布时间:");
		System.out.println("\t" + this.getPublishTime());
		System.out.println("  修改时间:");
		System.out.println("\t" + this.getModifyTime());
		System.out.println("  文章内容:");
		System.out.println("\t" + this.getContent());
		System.out.println("  所属板块:");
		System.out.println("\t" + this.getBoard());
		System.out.println("************************************************");
		System.out.println();
	}
}

Comment类:

package com.openlab.day13.exer1;

import java.util.Date;

public class Comment extends Blog {
	private int id; // 回复id
	private String content; // 内容
	private Author Author; // 作者
	private Date publishTime; // 发布时间
	private Date modifyTime; // 修改时间
	private Article article; // 文章

	@Override
	public String toString() {
		return content;
	}

	public Comment(int id, String content, Author Author, Date publishTime, Date modifyTime, Article article) {
		super();
		this.id = id;
		this.content = content;
		this.Author = Author;
		this.publishTime = publishTime;
		this.modifyTime = modifyTime;
		this.article = article;
	}

	public Comment() {
		super();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Author getAuthor() {
		return Author;
	}

	public void setAuthor(Author Author) {
		this.Author = Author;
	}

	public Date getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	@Override
	public void showPage() {
		// 文章详情页
		System.out.println("********************评论详情********************");
		System.out.println("  评论编号:");
		System.out.println("\t" + this.getId());
		System.out.println("  评论作者:");
		System.out.println("\t" + this.getAuthor());
		System.out.println("  发布时间:");
		System.out.println("\t" + this.getPublishTime());
		System.out.println("  修改时间:");
		System.out.println("\t" + this.getModifyTime());
		System.out.println("  评论内容:");
		System.out.println("\t" + this.getContent());
		System.out.println("  所属文章:");
		System.out.println("\t" + this.getArticle());
		System.out.println("************************************************");
		System.out.println();
	}
}

Test类:

package com.openlab.day13.exer1;

import java.util.Date;
import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Article article = new Article();
		Author author = new Author();
		Board board = new Board();
		Comment comment = new Comment();

		author.setId(100001);
		author.setName("zhang");
		author.setAge(20);
		author.setGender("男");
		author.setIntro("入门程序猿,请多指教!");

		board.setId(1);
		board.setName("Java");
		board.setIntro("Java面向对象编程,文章汇聚地");

		article.setId(1001);
		article.setTittle("面向对象三大特征");
		article.setAuthor(author);
		article.setPublishTime(new Date());
		article.setModifyTime(new Date());
		article.setContent("面向对象的三大特征主要为:封装、继承、多态\n" + "\t    1. 封装\n" + "\t    2. 继承\n" + "\t    3. 多态\n");
		article.setBoard(board);
//		article.showPage();

		comment.setId(1);
		comment.setAuthor(author);
		comment.setPublishTime(new Date());
		comment.setModifyTime(new Date());
		comment.setContent("作者,可以详细说一下什么是封装吗\n");
		comment.setArticle(article);
		while (true) {
			Blog page = null;
			System.out.println("		用户博客管理系统");
			System.out.println("************************************************");
			System.out.println();
			System.out.println("\t\t1. 模块详情");
			System.out.println();
			System.out.println("\t\t2. 文章详情");
			System.out.println();
			System.out.println("\t\t3. 作者详情");
			System.out.println();
			System.out.println("\t\t4. 评论详情");
			System.out.println();
			System.out.println("\t\t5. 退出");
			System.out.println();
			System.out.println("************************************************");
			System.out.print("请输入你的选项(1-5):");
			switch (scanner.nextInt()) {
			case Blog.Board:
				page = board;
				break;
			case Blog.Article:
				page = article;
				break;
			case Blog.Author:
				page = author;
				break;
			case Blog.Comment:
				page = comment;
				break;
			default:
				System.exit(1);
				break;
			}
			System.out.println();
			page.showPage();
		}
		
	}
}

结果:

Java编程练习题:面向对象练习_第2张图片

Java编程练习题:面向对象练习_第3张图片

Java编程练习题:面向对象练习_第4张图片

Java编程练习题:面向对象练习_第5张图片

Java编程练习题:面向对象练习_第6张图片

3. (抽象类)定义一个抽象的"Role"类 有姓名 年龄 性别等成员变量

1. 要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有) 
    再通过GetXXX()和SetXXX()方法对各变量进行读写。具有一个抽象的play()方法 
    该方法不返回任何值 同时至少定义两个构造方法。Role类中要体现出this的几种用法。
2. 从Role类派生出一个"Employee"类 该类具有Role类的所有成员 
    构造方法除外 并扩展 salary成员变量 同时增加一个静态成员变量“职工编号 ID ”。
    同样要有至少两个构造方法 要体现出this和super的几种用法 
    还要求覆盖play()方法 并提供一个final sing()方法。 
3. "Manager"类继承"Employee"类 有一个final成员变量"vehicle" 
4. 在main()方法中制造Manager和Employee对象,并测试这些对象的方法

Role类:

package com.openlab.day13.exer2;

public abstract class Role {
	private String name;
	private int age;
	private String gender;

	@Override
	public String toString() {
		return "姓名:" + name + ",年龄:" + age + ",性别:" + gender + "]";
	}

	public Role() {
		super();
	}

	public Role(String name, int age, String gender) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}
	
	public abstract void play();
}

Employee类:

package com.openlab.day13.exer2;

public class Employee extends Role {
	private double salary;
	private static int id = 1001;

	@Override
	public String toString() {
		return "姓名:" + super.getName() + ",工号:" + Employee.id + ",年龄:" + super.getAge() + 
				",性别:" + super.getGender() + ",薪水:" + this.salary;
	}

	public Employee() {
		super();
	}

	public Employee(String name, int id, int age, String gender, double salary) {
		super(name, age, gender); // 调用父类的构造方法,一定要放在方法的首个语句
		this.salary = salary;
		Employee.id = id;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public static int getId() {
		return id;
	}

	public static void setId(int id) {
		Employee.id = id;
	}

	@Override
	public void play() {
		System.out.println(super.getAge() + ",这个年纪就应该玩!");
;
	}
	
	public final void sing() {
		System.out.println(this.getName() + ",放声歌唱吧");
	}
}

Manager类:

package com.openlab.day13.exer2;

public class Manager extends Employee {
	private final String vehicle = "公交";
	
	@Override
	public String toString() {
		return "姓名:" + super.getName() + ",工号:" + super.getId() + ",年龄:" + super.getAge() + 
				",性别:" + super.getGender() + ",薪水:" + super.getSalary() + ",交通工具:" + this.vehicle;
	}

	public String getVehicle() {
		return vehicle;
	}
}

Test类:

package com.openlab.day13.exer2;

public class Test {
	public static void main(String[] args) {
		Manager manager = new Manager();
		manager.setAge(20);
		manager.play();
		manager.setName("zhang");
		manager.sing();
		manager.setGender("男");
		manager.setSalary(10000);
		System.out.println(manager);
		System.out.println(manager.getVehicle());
		
		Employee employee1 = new Employee();
		employee1.setName("wang");
		employee1.setAge(30);
		employee1.setGender("男");
		employee1.setSalary(4000);
		System.out.println(employee1);
		employee1.play();
		employee1.sing();
		Employee employee = new Employee("li", 1008, 25, "男", 5000);
		System.out.println(employee);
		employee.play();
		employee.sing();
		
		Role r1 = new Manager();
		r1.setAge(18);
		r1.play();
//		r1.sing();
		System.out.println(r1);
	}
}

结果:

Java编程练习题:面向对象练习_第7张图片

4. (抽象类)品尝饮料

1、建立一个Java抽象类Drink,应当

  • a、声明一个抽象方法taste() 该方法负责输出饮料的味道
  • b、声明int型常量来代表不同的饮料类型 咖啡、啤酒、牛奶
  • c、声明静态工厂方法getDrink(int drinkType) 根据传入的参数创建不同的饮料对象 并返回该对象 建议使用switch语句。该方法要求声明DrinkNotFoundException 当没有相对应的饮料类型时 抛出该异常。

2、建立Drink的具体子类

  • a、分别建立Drink的子类 Coffee 代表咖啡 Beer 代表啤酒 Milk 代表牛奶 ;
  • b、实现taste()方法 要求在控制台打印各自的味道特征。

3、建立异常类DrinkNotFoundException

  • a、继承Exception
  • b、在Drink的 方 法getDrink(int drinkType)中 声 明 引 发DrinkNotFoundException异常 当传入的参数没有相对应的饮料类型时 抛出异常。
  • c、在使用getDrink方法的类中捕捉该异常。

4、建立Test测试类 测试以上内容的正确性

  • a、编写main方法 通过命令行传参的方式传入某种饮料的类型。
  • b、在main方法中 调用Drink类的getDrink方法 获得相应的饮料对象。注意捕获DrinkNotFoundException。
  • c、然后调用该饮料对象的taste()方法 输出该饮料的味道。

5、编译程序 并运行。

注意事项:1、仔细审题 把题目要求理解准确 2、请注意多态性的运用 3、请注意代码的书写、命名符合规范 应有适当的注释

Drink类:

package com.openlab.day13.exer3;

public abstract class Drink {
	// 声明int型常量来代表不同的饮料类型 咖啡、啤酒、牛奶
	final static int COOFFEE = 1; // 咖啡
	final static int BEER = 2; // 啤酒
	final static int MILK = 3; // 牛奶
	
	// 声明静态工厂方法getDrink(int drinkType) 
	// 根据传入的参数创建不同的饮料对象,并返回该对象
	public static Drink getDrink(int drinkType) throws DrinkNotFoundException {
		switch (drinkType) {
		case COOFFEE:
			Coffee coffee = new Coffee();
			return coffee;
		case BEER:
			Beer beer = new Beer();
			return beer;
		case MILK:
			Milk milk = new Milk();
			return milk;
		default:
			throw new DrinkNotFoundException("找不的对应类型的饮料!");
		}
	}
	
	// 声明一个抽象方法taste(),该方法负责输出饮料的味道
	public abstract void taste();
}

Coffee类:

package com.openlab.day13.exer3;

public class Coffee  extends Drink {
	
	// 实现taste()方法
	@Override
	public void taste() {
		System.out.println("咖啡是厚重的苦味");
	}

}

Beer类:

package com.openlab.day13.exer3;

public class Beer extends Drink {

	// 实现taste()方法
	@Override
	public void taste() {
		// 打印各自的味道特征。
		System.out.println("啤酒是略微的苦涩味道");
	}

}

MilK类:

package com.openlab.day13.exer3;

public class Milk extends Drink {
	
	// 实现taste()方法
	@Override
	public void taste() {
		System.out.println("牛奶奶香味特别浓的");
	}

}

DrinkNotFoundException异常类:

package com.openlab.day13.exer3;

// 建立异常类DrinkNotFoundException,继承Exception 
public class DrinkNotFoundException extends Exception {
	static final long serialVersionUID = -3387516993124229948L;

	public DrinkNotFoundException(String message) {
		super(message);
	}
}

Test类:

package com.openlab.day13.exer3;

import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int i = 0;
		while (i < 4) {
			System.out.println("请输入你要和的饮料的类型(1.coffee,2.beer,3.milk):");
			int type = scanner.nextInt(); // 控制台输入饮料类型
			Drink drink1 = null;
			try {
				drink1 = Drink.getDrink(type);
			} catch (DrinkNotFoundException e) {
				// 异常捕获
				System.out.println("Exception thrown  :" + e);
				continue;
//				e.printStackTrace();
			}
			drink1.taste();  // 调用该饮料对象的taste
			i++;
		}
		scanner.close();
	}
}

结果:

Java编程练习题:面向对象练习_第8张图片

你可能感兴趣的:(Java编程题,java,servlet,数据结构)