Javase day04_汇总

面向对象

实体类

package com.neuedu.day04.homework;
/**
 * 设计一个类Student,该类包括姓名、学号和成绩。 设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息
 */
public class Student {
	private String name;
	private int no;
	private int score;

	public Student() {
		super();
	}

	public Student(String name, int no, int score) {
		super();
		this.name = name;
		this.no = no;
		this.score = score;
	}

	public void sort(Student[] st) {

		for (int i = 1; i < st.length; i++) {
			for (int j = 0; j < st.length - i; j++) {
				if (st[j].score < st[j + 1].score) {
					Student s = st[j];
					st[j] = st[j + 1];
					st[j + 1] = s;
				}
			}

		}
	}

	public String getName() {
		return name;
	}

	public int getNo() {
		return no;
	}

	public int getScore() {
		return score;
	}

}

业务层

package com.neuedu.day04;//业务类
/**
 * 
 * @author Administrator
 * aController 调度层 控制层 -与用户交互
 * Service 业务逻辑层(把某事需要几步1,2,3)
 * dao(专门操作数据表 仓库管理员)
 */
public class CarController {
	public void scheCar(String date) {
		System.out.println("开始车辆调度管理");
		// 调用其他业务类
		System.out.println("先查询一下当天车辆使用情况");
		System.out.println("看看哪个车当天有空");
		System.out.println("派车,记到日常表里");
	}
}

工具类

package com.neuedu.day04;//工具类

import java.util.UUID;

public class GenerateUUID {
	// 变量分3种
	// 1.Static修饰的属性可以被大家共享。谁都可以改变他的值,并且可以用类名,属性名直接访问到,而且他产生比较早,类存在他就存在
	// 2.全局(实例变量)类内共享,类内的方法都可以访问到
	// 3.局部变量,只有在方法内部生效,出了方法就不好用了
	public static int count = 1234;

	public static String uuid() {
		String newid = UUID.randomUUID().toString();
		System.out.println(newid);
		return newid;
	}
}

变量

package com.neuedu.day04;

import java.util.Date;

import org.junit.Test;

/**
 * public(公有的) private(私有的)只有当前类可以访问 方法 public 访问权限修饰符 void
 * 方法的返回值,期待他返回什么类型就写什么类型,如果没有返回值类型,就用void补位 setStuNo 方法名字 小驼峰形式 【做什么】 (int
 * StuNo) 参数列表 【需要什么材料】 参数可以是多个,多个之间用逗号隔开 每个参数分两部分写,(参数类型 形参名字) public void
 * setStuNo(int StuNo)
 */
public class StuTest {

	@Test
	public void myTest01() {
		// 类如何变成对象
		// 类名 对象名(任意起)=new类名();
		Student shenhao = new Student();// 类实例化出一个对象
		shenhao.setBirthday(new Date());
		shenhao.setClassno(2011);
		shenhao.setSex('男');
		shenhao.setStuName("申昊");
		shenhao.setStuNo(201101);

		System.out.println(shenhao.getSex());

		Student sunxin = new Student();
		shenhao.setBirthday(new Date());
		shenhao.setBirthday(new Date());
		sunxin.setClassno(2011);
		sunxin.setSex('男');
		sunxin.setStuName("孙鑫");
		sunxin.setStuNo(201102);
	}

	@Test
	public void myTest02() {
		Student shenhao = new Student(201101, "申昊", '男', new Date(), 2011, "");

	}

	@Test
	public void myTest03() {
		// 业务类其实主要是dosth
		CarController controller = new CarController();
		controller.scheCar("2018-02-08");
	}

	// 变量分3种
	// 1.Static修饰的属性可以被大家共享。谁都可以改变他的值,并且可以用类名,属性名直接访问到,而且他产生比较早,类存在他就存在
	// 2.全局(实例变量)类内共享,类内的方法都可以访问到
	// 3.局部变量,只有在方法内部生效,出了方法就不好用了

	@Test
	public void myTest04() {
		// 业务类其实主要是dosth
		GenerateUUID.uuid();// 32位字符串
		Math.round(123.46f);
		double pi = Math.PI;
		 GenerateUUID.count++;
		 System.out.println(GenerateUUID.count);

	}
	
}

作业

定义一个点类Point, 包含2个成员变量x、y分别表示x和y坐标,
2个构造方法Point()和Point(int x0,y0),
以及一个movePoint(int dx,int dy)方法 实现点的位置移动

package com.neuedu.day04.homework;


public class Point {
	// 横坐标
	private int x;
	// 纵坐标
	private int y;

	public Point() {
		super();
	}

	public Point(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	public void movePoint(int dx, int dy) {
		this.x += dx;
		this.y += dy;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

}
@Test
	public void myTest01() {
		Point p1 = new Point(100, 90);
		p1.movePoint(5, 5);
		System.out.println("p1的坐标为:" + p1.getX() + "," + p1.getY());
		Point p2 = new Point(80, 30);
		p2.movePoint(5, 5);
		System.out.println("p2的坐标为:" + p2.getX() + "," + p2.getY());
	}

设计一个类Student,该类包括姓名、学号和成绩。 设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息

package com.neuedu.day04.homework;


public class Student {
	private String name;
	private int no;
	private int score;

	public Student() {
		super();
	}

	public Student(String name, int no, int score) {
		super();
		this.name = name;
		this.no = no;
		this.score = score;
	}

	public void sort(Student[] st) {

		for (int i = 1; i < st.length; i++) {
			for (int j = 0; j < st.length - i; j++) {
				if (st[j].score < st[j + 1].score) {
					Student s = st[j];
					st[j] = st[j + 1];
					st[j + 1] = s;
				}
			}

		}
	}

	public String getName() {
		return name;
	}

	public int getNo() {
		return no;
	}

	public int getScore() {
		return score;
	}

}
@Test
	public void myTest2() {
		Student[] st = new Student[2];
		st[0] = new Student("申浩", 200001, 90);
		Student sunxin = new Student("孙鑫", 200002, 91);
		st[1] = sunxin;
		for (int i = 0; i < st.length; i++) {
			System.out.println(st[i].getName());
		}

	}

定义一个矩形类Rectangle: a)
定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。 b)
有2个属性:长length、宽width c) 通过构造方法Rectangle(int width, int length),分别给两个属性赋值 d)
创建一个Rectangle对象,并输出相关信息

package com.neuedu.day04.homework;


public class Rectangle {
	private int length;
	private int width;

	public Rectangle() {
		super();
	}

	public Rectangle(int length, int width) {
		super();
		this.length = length;
		this.width = width;
	}

	public int getArea(int length, int width) {
		int area = length * width;
		return area;

	}

	public int getPer(int length, int width) {
		int per = 2 * (length + width);
		return per;
	}

	public int getLength() {
		return length;
	}

	public int getWidth() {
		return width;
	}
	public void showAll(int length, int width,int area,int per){
		System.out.println("长:" + length);
		System.out.println("宽:" + width);
		System.out.println("面积:" + area);
		System.out.println("周长:" + per);
	}
	
}
@Test
	public void myTest3() {
		Scanner sc = new Scanner(System.in);
		int length = sc.nextInt();
		int width = sc.nextInt();
		Rectangle Re = new Rectangle(length, width);
		int area = Re.getArea(length, width);
		int per = Re.getPer(length, width);
		Re.showAll(length, width, area, per);
	}

定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 a) 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
b) 输出笔记本信息的方法 c) 然后编写一个测试类,测试笔记本类的各个方法。

package com.neuedu.day04.homework;

public class Notebook {
	private char color;
	private int cpu;

	public Notebook() {
		super();
	}

	public char getColor() {
		return color;
	}

	public void setColor(char color) {
		this.color = color;
	}

	public int getCup() {
		return cpu;
	}

	public void setCup(int cup) {
		this.cpu = cpu;
	}

	public Notebook(char color, int cpu) {
		super();
		this.color = color;
		this.cpu = cpu;
	}

	public void sysout(char color, int cpu) {
		System.out.println("笔记本颜色是:" + color);
		System.out.println("笔记本型号是:" + cpu);
	}
}
@Test
	public void myTest4() {
		Scanner sc = new Scanner(System.in);
		char color = sc.next().charAt(0);
		int cpu = sc.nextInt();
		Notebook n = new Notebook(color, cpu);
		n.sysout(color, cpu);
	}

定义一个人类Person:定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
,有三个属性:名字、身高、体重 。

package com.neuedu.day04.homework;
public class Person {
	private String name;
	private float heigth;
	private float weigth;

	public Person(String name, float heigth, float weigth) {
		super();
		this.name = name;
		this.heigth = heigth;
		this.weigth = weigth;
	}

	public  void sayHello(String name) {
		System.out.println("hello,my name is " + name);
	}
}
@Test
	public void myTest5() {
		Scanner sc = new Scanner(System.in);
		String name = sc.next();
		float heigth = sc.nextFloat();
		float weigth = sc.nextFloat();
		Person p = new Person(name, heigth, weigth);
		p.sayHello(name);
	}

定义一个PersonCreate类:创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74,分别调用对象的sayHello()方法。

package com.neuedu.day04.homework;

public class PersonCreate {
private String name;
private String age;
private float heigth;

public PersonCreate(String name, String age, float heigth) {
	super();
	this.name = name;
	this.age = age;
	this.heigth = heigth;
}

public  void sayHello(String name) {
	System.out.println("hello,my name is " + name);
}

public String getName() {
	return name;
}

public String getAge() {
	return age;
}

public float getHeigth() {
	return heigth;
}

}
@Test
	public void myTest6() {
		PersonCreate p = new PersonCreate("zhangsan", "33岁", 1.73f);
		PersonCreate p1 = new PersonCreate("lishi", "44岁", 1.74f);
		p.sayHello(p.getName());
		p1.sayHello(p1.getName());

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值