一.定义一个计算器;它的功能有加,减,乘,除,累加,阶乘,幂,平方
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("不能行走,此英雄已死亡的信息");
}
else
System.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 stub
Computer 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 stub
Person 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.0
public 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();
}
}