Java_2017.10.09
来源:互联网 发布:js跳转微信打开链接 编辑:程序博客网 时间:2024/06/05 14:29
1.定义一个关于交通的类。
在Vehicle类中
package Occ;public class Vehicle {double speed;double size;public void move() {System.out.println("要加油我才能走");}public void setSpeed(int speed) {System.out.println("初始化速度为" + speed);}public void speedUp() {speed += 111;System.out.println("已加速为" + speed);}public void speedDown() {speed -= 11;System.out.println("已减速为" + speed);}}在VehicleTest测试类中
package Occ;public class VehicleTest {public static void main(String[] args) {Vehicle v = new Vehicle();v.speed = 111;v.size = 22.2;v.move();v.setSpeed(111);v.speedUp();v.speedDown();}}/*要加油我才能走初始化速度为111已加速为222.0已减速为211.0*/
2.定义一个梯形类
package work01;public class Tixing {double upper, lower, high;public Tixing(double upper, double lower, double high) {this.upper = upper;this.lower = lower;this.high = high;}double getArea() {return (upper + lower) * high / 2;}public double getUpper() {return upper;}public void setUpper(double upper) {this.upper = upper;}public double getLower() {return lower;}public void setLower(double lower) {this.lower = lower;}public double getHigh() {return high;}public void setHigh(double high) {this.high = high;}public String toString() {return "Tixing [upper=" + upper + ", lower=" + lower + ", high=" + high+ "]";}}
package work01;public class TestTixing {public static void main(String[] args) {Tixing t1 = new Tixing(10, 20, 10);System.out.println(t1.upper + " " + t1.lower + " " + t1.high);System.out.println(t1.getArea());System.out.println(t1.toString());t1.setHigh(100);System.out.println(t1.toString());Tixing t2 = new Tixing(20, 40, 20);System.out.println(t2.toString());t2 = t1;System.out.println(t2.toString());System.out.println(t1.toString());t1.setHigh(50);System.out.println(t2.toString());System.out.println(t1.toString());// 简单变量赋值后处理int i = 10;int j = i;System.out.println(i + " " + j);i = 20;System.out.println(i + " " + j);}}/*10.0 20.0 10.0150.0Tixing [upper=10.0, lower=20.0, high=10.0]Tixing [upper=10.0, lower=20.0, high=100.0]Tixing [upper=20.0, lower=40.0, high=20.0]Tixing [upper=10.0, lower=20.0, high=100.0]Tixing [upper=10.0, lower=20.0, high=100.0]Tixing [upper=10.0, lower=20.0, high=50.0]Tixing [upper=10.0, lower=20.0, high=50.0]10 1020 10*/
3.定义一个学生类
编程定义一个学生类包括成员变量 xm(姓名)、xh(学号)、xb(性别);成员方法有构造方法(不带参数和带三个参数),设置和获取上述三个成员变量的方法,以及打印出姓名、学号与性别的print()方法、toString()方法。
package work01;public class Student {String xm,xh,xb;public Student(){}public Student(String xm,String xh,String xb){this.xm=xm;this.xh=xh;this.xb=xb;}public void setXm(String xm){this.xm=xm;}public String getXm(){return xm;}public void setXh(String xh){this.xh=xh;}public String getXh(){return xh;}public void setXb(String xb){this.xb=xb;}public String getXb(){return xb;}public String toString(){return "(toString)姓名:"+xm+" 学号:"+xh+" 性别:"+xb;}public static void main(String[] args) {Student stu0=new Student();stu0.xm="java";Student stu=new Student("wxy","222","女");System.out.println(stu0.toString());System.out.println("姓名:"+stu.xm+" 学号:"+stu.xh+" 性别:"+stu.xb);System.out.println(stu.toString());stu.setXh("214");stu.setXm("lm");stu.setXb("男");System.out.println(stu.toString());}}/*(toString)姓名:java 学号:null 性别:null姓名:wxy 学号:222 性别:女(toString)姓名:wxy 学号:222 性别:女(toString)姓名:lm 学号:214 性别:男*/
4.定义一个复数类
package work01;public class Complex {double real, ima;public Complex() {}public Complex(double real, double ima) {this.real = real;this.ima = ima;}public double getReal() {return real;}public double getIma() {return ima;}public void setReal(double real) {this.real = real;}public void setIma(double ima) {this.ima = ima;}public Complex add(Complex c) {double newreal = this.real + c.getReal();double newima = this.ima + c.getIma();Complex ans = new Complex(newreal, newima);return ans;}public Complex sub(Complex c) {double newreal = this.real - c.getReal();double newima = this.ima - c.getIma();Complex ans = new Complex(newreal, newima);return ans;}public String toString() {if (this.ima == 0) {return "" + this.real;} else if (this.ima > 0) {return this.real + "+" + this.ima + "i";} else {return this.real + "" + this.ima + "i";}}public static void main(String[] args) {Complex a = new Complex(1, 2);Complex b = new Complex(9, 9);System.out.println(a.add(b));System.out.println(a.sub(b));}}/* * 10.0+11.0i * -8.0-7.0i */
阅读全文
0 0
- Java_2017.10.09
- java_2017.9.25
- java_2017.9.18
- java_2017.10.10
- 牛客网刷题之JAVA_2017.3.21
- java_2017.11.1_继承 接口
- java_2017.10.30_行星的转动
- 10.09.05 ~ 10.09.11
- 10.09.12 ~ 10.09.18
- 10.09.19 ~ 10.09.25
- 10.08.29 ~ 10.09.04
- 超级旋风1.6.140发布(10.09)
- count logs from 10.09 to 10.10
- 11级_Java_曹建波 10.09 XML约束
- 10.09国际财经解读,行情分析
- #USACO 10.09 Bessie的体重问题
- 个人回忆录之一年总结(09.09-10.09)
- mac 10.09之后文件的显示隐藏终端命令
- C++一本通题库1023
- HashMap底层实现原理
- 数据库连接错误Cannot create PoolableConnectionFactory
- 百度首页图标 缩放+旋转 变形
- iOS内存浅谈
- Java_2017.10.09
- Uncaught Error: Error calling method on NPObject.
- 十图详解tensorflow数据读取机制(附代码)
- C++一本通题库1024
- POJ-3461-Oulipo
- 什么是进程?什么是线程?进程与线程有何区别?
- C++一本通题库1025
- Class.forName(String className) 实例化对象问题
- 045 中值定理总结(罗尔定理,拉格朗日定理,柯西定理,泰勒公式)及型一二三四五