Homework20191108

作业

  1. /*创建一个Student 类 包含属性有 序号 姓名  年龄  生日  

 * 提供对应的setter/getter

创建对象,完成赋值(setter赋值和构造方法赋值)  

在控制台输出学生对象的信息,要求生日的格式为 yyyy-MM-dd*/

import java.text.SimpleDateFormat;

import java.util.Date;

public class Test01 {

 

public static void main(String[] args) {

Student stu = new Student(001,"熊落落",21);

 

    Date date = new Date();

    date.setYear(98);//1900+98

    date.setMonth(0);//0-11

    date.setDate(2);

stu.setBirth(date);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String birthday = sdf.format(stu.getBirth());//yyyy-MM-dd格式的生日

 

System.out.println("序号:" + stu.getId());

System.out.println("姓名:" + stu.getName());

System.out.println("年龄:" + stu.getAge());

System.out.println("生日:" + birthday);

 

 

}

 

}

 

/*创建一个Student 类 包含属性有 序号 姓名  年龄  生日  

 * 提供对应的setter/getter

创建对象,完成赋值(setter赋值和构造方法赋值)  

在控制台输出学生对象的信息,要求生日的格式为

 yyyy-MM-dd*/

class Student{

private int id;//序号

private String name;//姓名

private int age;//年龄

private Date birth;//生日

//无参构造方法

public Student() {

super();

// TODO Auto-generated constructor stub

}

//有参构造方法

public Student(int id, String name, int age) {

super();

this.id = id;

this.name = name;

this.age = age;

}

//获取id属性

public int getId() {

return id;

}

//设置id属性

public void setId(int id) {

this.id = id;

}

//获取name属性

public String getName() {

return name;

}

//设置name属性

public void setName(String name) {

this.name = name;

}

//获取年龄属性

public int getAge() {

return age;

}

//设置年龄属性

public void setAge(int age) {

this.age = age;

}

//获取生日属性

public Date getBirth() {

return birth;

}

//设置生日属性

public void setBirth(Date birth) {

this.birth = birth;

}

 

}

2./*其实我个人觉得生日那个格式输出的时候调一下就行了

也不知道为啥搞那么复杂,啧啧啧*/

import java.text.SimpleDateFormat;

import java.util.Date;

/*创建一个Student 类 包含属性有 序号 姓名  年龄  生日  

 * 提供对应的setter/getter

创建对象,完成赋值(setter赋值和构造方法赋值)  

在控制台输出学生对象的信息,要求生日的格式为

 yyyy-MM-dd*/

public class Test01 {

 

public static void main(String[] args) {

Student stu = new Student(001,"熊落落",21);

 

    Date date = new Date();

    date.setYear(98);//1900+98

    date.setMonth(0);//0-11

    date.setDate(2);

stu.setBirth(date);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String birthday = sdf.format(stu.getBirth());//yyyy-MM-dd格式的生日

 

System.out.println("序号:" + stu.getId());

System.out.println("姓名:" + stu.getName());

System.out.println("年龄:" + stu.getAge());

System.out.println("生日:" + birthday);

 

//第二题十年后的生日

Date d = stu.toTenDate();

//System.out.println(d);

String tenBirth = sdf.format(d);

System.out.println("十年后生日:" + tenBirth);

 

}

 

}

 

/*创建一个Student 类 包含属性有 序号 姓名  年龄  生日  

 * 提供对应的setter/getter

创建对象,完成赋值(setter赋值和构造方法赋值)  

在控制台输出学生对象的信息,要求生日的格式为

 yyyy-MM-dd*/

class Student{

private int id;//序号

private String name;//姓名

private int age;//年龄

private Date birth;//生日

/*根据上一题, 在学生类中添加一个方法,

完成 返回 学生在10岁生日时的日期*/

public Date toTenDate() {

int year = birth.getYear();

birth.setYear(year+10);//增加十年

Date date = birth;

return date;

}

//无参构造方法

public Student() {

super();

// TODO Auto-generated constructor stub

}

//有参构造方法

public Student(int id, String name, int age) {

super();

this.id = id;

this.name = name;

this.age = age;

}

//获取id属性

public int getId() {

return id;

}

//设置id属性

public void setId(int id) {

this.id = id;

}

//获取name属性

public String getName() {

return name;

}

//设置name属性

public void setName(String name) {

this.name = name;

}

//获取年龄属性

public int getAge() {

return age;

}

//设置年龄属性

public void setAge(int age) {

this.age = age;

}

//获取生日属性

public Date getBirth() {

return birth;

}

//设置生日属性

public void setBirth(Date birth) {

this.birth = birth;

}

 

}

3 .import java.math.BigDecimal;

//做一个计算器类,对浮点数进行 加减乘除的精确运算

public class Test03 {

 

public static void main(String[] args) {

double b1 = 2.333;

double b2 = 2.444;

 

System.out.println("普通计算:" + (b1+b2));

System.out.println("BigDecimal计算:" + Calc.Add(b1, b2));

 

System.out.println("普通计算:" + (b1-b2));

System.out.println("BigDecimal计算" + Calc.Sub(b1, b2));

 

System.out.println("普通计算:" + (b1*b2));

System.out.println("BigDecimal计算:" + Calc.Mul(b1, b2));

 

System.out.println("普通计算:" + (b1/b2));

System.out.println("BigDecimal计算:" + Calc.Div(b1, b2));

//计算结果仿照电脑计算器

 

}

 

}

 

//做一个计算器类,对浮点数进行 加减乘除的精确运算

class Calc{

private static final int DEF_DIV_SCALE = 15;

//加法

public static double Add(double a,double b) {

BigDecimal b1 = new BigDecimal(Double.toString(a));

BigDecimal b2 = new BigDecimal(Double.toString(b));

return b1.add(b2).doubleValue();

}

//减法

public static double Sub(double a,double b) {

BigDecimal b1 = new BigDecimal(Double.toString(a));

//不知道为什么要转成字符串,但是貌似转成字符串之后,计算更精确一些

BigDecimal b2 = new BigDecimal(Double.toString(b));

return b1.subtract(b2).doubleValue();

}

//乘法

public static double Mul(double a,double b) {

BigDecimal b1 = new BigDecimal(Double.toString(a));

BigDecimal b2 = new BigDecimal(Double.toString(b));

return b1.multiply(b2).doubleValue();

}

//除法(精确到小数点后面十五位)

public static double Div(double a,double b) {

BigDecimal b1 = new BigDecimal(Double.toString(a));

BigDecimal b2 = new BigDecimal(Double.toString(b));

return b1.divide(b2,DEF_DIV_SCALE,BigDecimal.ROUND_HALF_UP).doubleValue();

}

 

}

4 练习使用Math中的各个方法

public class Test01Math {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

double d1 = Math.abs(33.6);

System.out.println(d1);

double d2 = Math.abs(-9);

System.out.println(d2);

double d3 = Math.ceil(4.1);

System.out.println(d3);

double d4 = Math.floor(-2.7);

System.out.println(d4);

double d5 = Math.round(5.4);

System.out.println(d5);

 

}

 

}

 

5. package com.xiongluoluo.homework;

 

import java.util.Random;

 

//使用Math 类 和 Random类 两种方式生成 20 到30之间的随机整数

public class Test05 {

 

public static void main(String[] args) {

int math = getMathRandom();

int random = getRandom();

System.out.println(math+"   " + random);

 

}

//方法一Math类

public static int getMathRandom(){

int number = (int)(Math.random()*10+20);//[20,30)

return number;

}

//方法二Random类

public static int getRandom() {

Random r = new Random();

int number = r.nextInt(10)+20;

return number;

}

 

}

 

 

6 递归作业:

递归输出

 

 

 

   方式一: 使用一层循环(循环输入每层的 空格和星)

 

   方式二: 不使用循环  使用三个方法递归,  

方法1  递归输出 空格   

方法2  递归输出 星

            方法3  递归调用上面两个方法 输出指定层数的要求图形

//方法一没什么问题

//方法二也就是递归的那个不太明白,emmm然后写的也不对QAQ

public class Test06 {

 

public static void main(String[] args) {

printStar01();

printSpace(4);

System.out.println("lalal");

printShape(5);

}

//方法一

public static void printStar01(){

for(int n1 = 1;n1 <= 5;n1++) {

for(int n2 = 1;n2 <= 5-n1;n2++) {

System.out.print(" ");

}

for(int n3 = 1;n3<=2*n1-1;n3++) {

if(n3%2==0) {

System.out.print(" ");

}else {

System.out.print("*");

}

}

System.out.println();

}

}

/*方式二: 不使用循环  使用三个方法递归,  

方法1  递归输出 空格   

方法2  递归输出 星

方法3  递归调用上面两个方法 输出指定层数的要求图形*/

//方法二

 

public static void printSpace(int n) {

if(n>1) {

printSpace(n-1);

}

System.out.print(" ");

}

 

public static void printStar(int n) {

if(n>1) {

printStar(n-1);

}

System.out.print("*");

}

 

 

 

public static void printShape(int n) {

 

 

}

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值