面向对象基础+构造方法+重载练习
一.定义一个计算器;它的功能有加,减,乘,除,累加,阶乘,幂,平方
package com.moju;public class Computer {//定义一个计算器;它的功能有// 加,减,乘,除,累加,阶乘,幂,平方//1.加法public int add(int a,int b) {return a+b; }//2.减法public int sub(int a,int b) {return a-b; }//3.乘法public int mul(int a,int b) {return a*b; }//4.除法public double div(double a,double b) {return a/b; }//5.累加public int accu(int a,int b) {int sum=0;for(int i=a;i<=b;i++) {sum+=i;}return sum;}//6.阶乘public int jiec(int a){int sum=1;for(int i = 1;i<=a;i++) {sum*=i;}return sum;}//7.平方public int pingf(int a) {return a*a;}//8.幂public int mi(int a,int b) {int sum=1;for(int i = 1;i<=b;i++) {sum=sum*a;}return sum;}
}//测试
package com.moju;public class Opt_test {public static void main(String[] args) {// TODO Auto-generated method stub//方法的参数及返回值// 定义一个计算器;它的功能有// 加,减,乘,除,累加,阶乘,幂,平方Computer c = new Computer();System.out.println(c.add(1, 2));System.out.println(c.sub(1, 2));System.out.println(c.div(1, 2));System.out.println(c.mul(1, 2));System.out.println(c.pingf( 2));System.out.println(c.accu(1, 5));System.out.println(c.jiec(2));System.out.println(c.mi(2, 2));}}
二.定义一个汽车类(Car),属性有颜色color,品牌brand,车牌号number,价格price,并实例化(new)一个对象,给属性赋值,并输入属性值,然后输出
public class Car {String color;String brand;String id;double price;public static void main(String[] args) {Car car1 = new Car();car1.brand = "奥迪";car1.color = "白色";car1.id = "黑A88888";car1.price = 234133.12;System.out.println(car1.brand+" "+car1.color+" "+car1.ID+" "+car1.price);}
}
三.定义一个球员类(Player),属性有身高,体重,姓名,实例化(new)两个球员,分别是姚明和科比;
public class Player {String name;double height;double weight;public static void main(String[] args) {Player player1 = new Player();player1.name = "姚明";player1.height = 2.3;player1.weight = 75;Player player2 = new Player();player2.name = "科比";player2.height = 2.1;player2.weight = 80;System.out.println(player1.name+"身高"+player1.height+"米,体重"+player1.weight+"公斤");System.out.println(player2.name+"身高"+player2.height+"米,体重"+player2.weight+"公斤");}}
三.定义一个僵尸类(Zombie),属性有名子,体力值,攻击力,实例化三个僵尸类,并给属性赋值;
public class Zombie {String name;int power;int attack;public static void main(String[] args) {Zombie zombie1 = new Zombie();zombie1.name = "普通僵尸";zombie1.power = 2000;zombie1.attack = 2000;Zombie zombie2 = new Zombie();zombie2.name = "铁桶僵尸";zombie2.power = 3000;zombie2.attack = 3000;Zombie zombie3 = new Zombie();zombie3.name = "盗贼僵尸";zombie3.power = 4000;zombie3.attack = 4000;System.out.println("名字\t"+"体力值\t"+"攻击力\t");System.out.println(zombie1.name+"\t"+zombie1.power+"\t"+zombie1.attack+"\t");System.out.println(zombie2.name+"\t"+zombie2.power+"\t"+zombie2.attack+"\t");System.out.println(zombie3.name+"\t"+zombie3.power+"\t"+zombie3.attack+"\t");}
四.打印int,char, float,double,String ,boolean这些数据类型作为类属性时的默认值
public class Default {int i;char ch;float f;double d;String st;boolean b;public static void main(String[] args) {Default exam = new Default();System.out.println("int类型的默认值为:"+exam.i);System.out.println("char类型的默认值为:"+exam.ch);//空System.out.println("float类型的默认值为:"+exam.f);System.out.println("double类型的默认值为:"+exam.d);System.out.println("String类型的默认值为:"+exam.st);System.out.println("boolean类型的默认值为:"+exam.b);}
}
五.设计一个立方体类Box,定义三个属性,分别是长,宽,高。定义二个方法,分别计算并输出立方体的体积size(长*宽*高)
public class Box {//设计一个立方体类Box,定义三个属性,分别是长,宽,高。定义二个方法,分别计算并输出立方体的体积和表面积int length;int width;int heigth;int Volume(Box box) {return box.heigth*box.length*box.width;}int SurfaceArea(Box box) {return (box.length*box.width+box.length*box.heigth+box.heigth*box.width)*2;}public static void main(String[] args) {Box box = new Box();box.heigth = 3;box.length = 4;box.width = 5;System.out.println("长方体的体积:"+box.Volume(box));System.out.println("长方体的表面积:"+box.SurfaceArea(box));}}
六.
请定义一个交通工具(Vehicle)的类,其中有:
属性:速度(speed),体积(size)等
方法:移动(move(int 秒)),设置速度(setSpeed(int s)),加速speedUp(),减速speedDown()等等.
最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给它初始化speed,size的值,并且通过方法打印出来。另外,调用加速,减速的方法对速度进行改变。调用 move方法输出移动距离
public class Vehicle {//定义一个交通工具(Vehicle)的类,其中有://属性:速度(speed),体积(size)等等//方法:移动(move(int s)),设置速度(setSpeed(int speed)),加速speedUp(),减速speedDown()等等.//最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给它初始化speed,size的值,并且通过打印出来。//另外,调用加速,减速的方法对速度进行改变。调用 move方法输出移动距离int speed;int size;void move(int s){System.out.println("在"+s+"s内,车子移动了"+this.speed*s+"米");}void setSpeed(int speed){this.speed = speed;}void setSize(int size){this.size = size;}void speedUp() {this.speed+=10;}void speedDown(){this.speed-=10;}public static void main(String[] args) {Vehicle vehicle = new Vehicle();vehicle.setSize(20);vehicle.setSpeed(110);vehicle.speedUp();vehicle.speedUp();vehicle.speedUp();vehicle.speedDown();vehicle.move(10);}}
七.定义一个Hero类
属性有 power,name,分别代表体力值和英雄的名子,体力值默认为100;
void go(); //行走的方法,如果体力值为0,则输出不能行走,此英雄已死亡的信息 void eat(int n); //吃的方法,参数是补充的血量,将 n的值加到属性power中,power的值最大为100, void hurt();//每受到一次伤害,体力值-10,体力值最小不能小于0
public class Hero {int power = 100;String name;void go() {if(this.power<=0) {System.out.println("不能行走,此英雄已死亡的信息");}elseSystem.out.println("Hero向前走了100米");} //行走的方法,如果体力值为0,则输出不能行走,此英雄已死亡的信息void eat(int n) {System.out.print("Hero吃了个血包,体力值加"+n);this.power+=n;if(this.power>100) {this.power = 100;}System.out.println(" 此时体力值为:"+this.power);} //吃的方法,参数是补充的血量,将 n的值加到属性power中,power的值最大为100,void hurt(){this.power-=10;if(this.power<=0) {System.out.print("Hero受到伤害,体力值减10");System.out.println(" 此时体力值为:"+this.power);this.go();}else {System.out.print("Hero受到伤害,体力值减10");System.out.println(" 此时体力值为:"+this.power);}}public static void main(String[] args) {Hero hero = new Hero();hero.go();hero.eat(10);hero.eat(20);hero.go();hero.hurt();hero.hurt();hero.hurt();hero.hurt();hero.hurt();hero.hurt();hero.hurt();hero.hurt();hero.hurt();hero.hurt();}}
八.定义一个计算器;它的功能有加,减,乘,除,累加,阶乘,求平方,求次方,判断一个数是否为素数;并写测试类来测试这个方法
package com.moju;import java.util.Arrays;public class Computer {//1.加法public int plus(int a,int b) {return a+b; }//2.减法public int minus(int a,int b) {return a-b; }//3.乘法public int multiply(int a,int b) {return a*b; }//4.除法public double divide(double a,double b) {if(b==0) {System.out.println("除数不能为0");return 0;}else {return a/b; }}//5.累加public int accu(int a,int b) {int sum=0;for(int i=a;i<=b;i++) {sum+=i;}return sum;}//6.阶乘public int jiec(int a){int sum=1;for(int i = 1;i<=a;i++) {sum*=i;}return sum;}//7.平方public int square(int a) {return a*a;}//8.幂public int power(int a,int b) {int sum=1;for(int i = 1;i<=b;i++) {sum=sum*a;}return sum;}//9. 是否是素数public void isPrime(int a) {boolean b=false;for(int i = 2;i<=a-1;i++) {if(a%i==0) {b = true;break;}}if(!b) {System.out.println(a+"是素数");}else {System.out.println(a+"不是素数");}}//10.排序public void sort(int[] arr) {int t;for(int i = 0;i <arr.length; i++){for(int j = i ; j > 0 ; j--){if(arr[j] < arr[j-1]){t = arr[j];arr[j] = arr[j-1];arr[j-1] = t;}else {break;}}}System.out.print("排序后: "+Arrays.toString(arr));}
}package com.moju;import java.util.Arrays;public class Test {public static void main(String[] args) {// TODO Auto-generated method stubComputer c = new Computer();System.out.println(c.plus(12,999));System.out.println(c.minus(100,999));System.out.println(c.multiply(12,999));System.out.println(c.divide(12,4));System.out.println(c.divide(12,0));System.out.println(c.square(12));System.out.println(c.power(12,2));c.isPrime(12);c.isPrime(2);System.out.println( c.accu(1, 3));System.out.println(c.jiec(3));int []arr = {1,3,2,4,3,1};c.sort(arr);}}
九.编写Java程序,用于显示人的姓名和年龄。
定义一个人类(Person),该类中应该有两个属性,姓名(name)和年龄(age)。定义构造方法,用来初始化数据成员。再定义显示(display)方法,将姓名和年龄打印出来。
在main方法中创建人类的实例,然后将信息显示。
package Object;
//定义一个人类(Person),该类中应该有两个属性,姓名(name)和年龄(age)。
//定义构造方法,用来初始化数据成员。再定义显示(display)方法,将姓名和年龄打印出来。
//在main方法中创建人类的实例,然后将信息显示。
public class Person {public Person(String name,int age) {this.name = name;this.age = age;}private String name;private int age;public void display() {System.out.println("姓名: "+name+"\n"+"年龄: "+age);}public static void main(String[] args) {// TODO Auto-generated method stubPerson p = new Person("zhangsan",10);p.display();}}
十.无名粉
package Object;public class WuMingFen {public WuMingFen(String theMa,int quantity,boolean likeSoup) {this.theMa = theMa;this.quantity = quantity;this.likeSoup = likeSoup;}public WuMingFen(String theMa,int quantity) {this.theMa = theMa;this.quantity = quantity;}private String theMa;//面码private int quantity;//粉的份量(两)private boolean likeSoup;//是否带汤public void check() {System.out.println("面码: "+theMa);System.out.println("粉的份量(两): "+ quantity);System.out.println("是否带汤: "+likeSoup);}public static void main(String[] args) {//粉对象是酸辣面码、2两、带汤的WuMingFen f1 = new WuMingFen("酸辣面",2,true);f1.check();}}
十一.写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。
a. 声明三个私有对象属性:firstName、lastName和account。
b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)
c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。
d. 声明setAccount 方法来对account属性赋值。
e. 声明getAccount 方法以获取account属性。
3.写一个测试程序。
- 创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
- 对Jane Smith操作。
存入 100 元,再取出960元。再取出2000元。
- 打印出Jane Smith 的基本信息
package Object;
public class Account {
public Account (int id, double balance, double annualInterestRate ) {this.id = id;this.balance = balance;this.annualInterestRate = annualInterestRate;}
//账号id,余额balance,年利率annualInterestRate;private int id;private double balance;private double annualInterestRate;public int getId() {return id;}public double getBalance() {return balance;}public double getAnnualInterestRate() {return annualInterestRate;}public void setId( int id) {this.id = id;}public void setBalance(double balance) {this.balance = balance;}public void setAnnualInterestRate(double annualInterestRate) {this.annualInterestRate = annualInterestRate;}//取款public double withdraw (double outer) {if(balance<outer) {System.out.println("余额不足");}else {balance -= outer;System.out.println("余额为: "+balance);}return balance;}//存款public void deposit (double amount) {balance += amount;}}package Object;public class Customer {public Customer(String f,String l) {this.firstName=f;this.lastName=l;}private String firstName;private String lastName;//类 类型 顾客"有一个"账号private Account account;public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public Account getAccount() {return account;}public void setAccount(Account account) {this.account = account;}public void setFirstName(String firstName) {this.firstName = firstName;}public void setLastName(String lastName) {this.lastName = lastName;}//打印个人信息//Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%,//balance is 1140.0public void info() {//属性私有化,只能用get方法System.out.println("Customer ["+this.lastName+","+this.firstName+"] has a account: id is "+this.getAccount().getId()+","+"annualInterestRate is "+this.getAccount().getAnnualInterestRate()*100+"%");}}package Object;public class Account_test {public static void main(String[] args) {/*
(1)创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
(2)对Jane Smith操作。
存入 100 元,再取出960元。再取出2000元。
(3)打印出Jane Smith 的基本信息*/Customer customer = new Customer("Jane","Smith");Account account = new Account(1000,2000,0.0123);//创建联系,顾客有id为1000的账号customer.setAccount(account);//不能直接操作account,这才是jane的账号Account account2 = customer.getAccount();account2.deposit(100);System.out.println(account2.getBalance());account2.withdraw(960);System.out.println(account2.getBalance());account2.withdraw(2000);customer.info();}}
面向对象基础+构造方法+重载练习相关推荐
- 面向对象基础——方法重载
小结 方法重载是指多个方法的方法名相同,但各自的参数不同: 重载方法应该完成类似的功能,参考String的indexOf(): 重载方法返回值类型应该相同. 在一个类中,我们可以定义多个方法.如果有一 ...
- JavaSE——面向对象基础(思想、类与对象、构造方法、重载、匿名对象)
第1节 面向对象基础 一.面向对象思想 1.1 概述 面向对象(Object Oriented)是软件开发方法.面向对象的概念和应用已超越了程序设计和软件开发,是一种对现实世界理解和抽象的方法,是计算 ...
- Java面向对象之构造方法、构造方法重载
文章目录 一.构造方法 二.构造方法重载 一.构造方法 1.简单说明 在创建对象时,自动调用的方法为构造方法,它没有返回值,最大的作用就是用来存放类的属性信息(存放.转换).同时,在定义类的时候,ja ...
- PHP面向对象基础总结
近来参加了几场PHP工程师的面试,但是笔试题答得都不理想,回来总结了一下失败的原因,是没看PHP手册.几家公司的PHP基础面试题都可以在PHP手册上找到.哎,现在才知道最好的面试宝典是PHP手册. 下 ...
- java面向对象课件_《JAVA面向对象基础》PPT课件.ppt
<<JAVA面向对象基础>PPT课件.ppt>由会员分享,可在线阅读,更多相关<<JAVA面向对象基础>PPT课件.ppt(68页珍藏版)>请在人人文库 ...
- Java大数据-Week2-Day1 面向对象基础
第三章第二节 面向对象基础 文章目录 第三章第二节 面向对象基础 前言 类与对象的创建 方法的补充 对象创建补充 对象创建内存01 对象创建内存02 对象创建内存03 构造方法 方法的重载 构造方法重 ...
- 第六章、面向对象基础--中(续)构造器、this、包、eclipse的使用
文章目录 内容 学习目标 第六章 面向对象基础--中(续) 6.2 构造器(Constructor) 构造器的作用 构造方法的定义格式 注意事项 练习 6.3 this关键字 this的含义 this ...
- 【Java】Java学习笔记(2)——Java面向对象基础作业函数题
本人私人博客:Megalomania,大部分文章会现在博客上传,有不足之处欢迎指正. 学校小学期Java课程的练习题,留个档便于以后需要时候有例子可以回忆,写的烂的地方请多多包含 1.求两个数值之和 ...
- Java学习(二)面向对象基础
Java面向对象基础 一.面向对象思想 1.编程语言的发展 2.什么是类?什么是对象? 3.面向对象特征 二.类与对象 1.类的声明 2.系统类库 3.访问控制 4.对象的创建和使用 三.方法 1.构 ...
最新文章
- GLSL实现滤镜效果
- Linux线程时间片如何修改,请教如何修改线程时间片
- 3、常用关键字,变量赋值,多个变量赋值,标准数据类型,数字,字符串,列表,元组,字典,数据类型转换
- Symantec防病毒企业版10.1部署方法一
- 在SAP C4C TI(Thing Inspector)页面里添加自定义UI
- 数组字典_VBA数组与字典解决方案第34讲:数组的传递
- 互联网晚报 | 2月18日 星期五 | 高途宣布停止高中学科辅导服务;小红书启动最严医美专项治理;FF 91量产版2月23日发布...
- catia圆柱转化为圆台_中考难点,最值问题之构造与转化
- HttpRequest Get和Post调用其他页面的方法
- php pdo oci8,PHP516 用phpize增加扩展PDO_OCI和OCI8
- 微信小程序与公众号推送消息
- (四) 制作一个最简单的qt界面
- 毕业设计的一些小总结
- 信息安全技术——(十五)物联网关键技术
- PostgreSQL on Linux 最佳部署手册
- nnunet 扩充流程
- HMM(隐马尔可夫)
- ssm毕设项目学生宿舍管理系统15pjb(java+VUE+Mybatis+Maven+Mysql+sprnig)
- A2 AP AUTOSAR 与 CP AUTOSAR 的特性
- 聚类dbi指数_聚类算法