1.java定义一个电视机类,实现电视机的基本功能。
1.先定义一个电视机类和方法。
public class TV{
int tvchannel =0;
int tvvolume =0;
int tvswitch =0;
public void ChangeChannel(int tvchannel){
this.tvchannel=tvchannel;
System.out.println("现在是"+tvchannel+"台");
}
public void VolumeAdjustment(int changevolume){
if(changevolume==1){
if (tvvolume>=3){
System.out.println("已经调到最大音量!");
return;
}
this.tvvolume++;
}
else if (changevolume==0){
if (tvvolume<=0){
System.out.println("已经调整到最小的音量!");
return;
}
this.tvvolume--;
}
else
System.out.println("ERROR!");
System.out.println("现在音量是"+this.tvvolume);
}
public void Switch(int tvswitch){
if (tvswitch==1){
System.out.println("打开电视!");
}
else if (tvswitch==0){
System.out.println("关闭电视!");
}
else
System.out.println("ERROR!");
this.tvswitch=tvswitch;
}
}
2.调用类和方法。
import java.util.Scanner;
public class TV2 {
public static void main(String[] args){
TV tv=new TV();
System.out.println("电视处于关闭!");
int select=1;
Scanner in =new Scanner(System.in);
System.out.println("1.打开电视!");
System.out.println("0.离开!");
System.out.println("请选择:");
select = in.nextInt();
tv.Switch(select);
if (select==0){
System.out.println("已退出!");
return;
}
else {
while (select!=0){
System.out.println("1.换台!");
System.out.println("2.调音量");
System.out.println("0.关电视!");
System.out.println("请选择:");
select=in.nextInt();
switch (select){
case 1:
int tvchannel=0;
System.out.println("请输入台数:");
tvchannel=in.nextInt();
tv.ChangeChannel(tvchannel);
break;
case 2:
int tvvolume=0;
System.out.println("1.增大音量!");
System.out.println("0.减小音量!");
tvvolume=in.nextInt();
tv.VolumeAdjustment(tvvolume);
break;
case 0:
tv.Switch(select);
System.out.println("已退出!");
break;
}
}
}
}
}
2.设计一个分数类,分数的分子和分母用两个整型数表示。
类中定义方法对分数进行加、减、乘、除运算。定义分数类对象,运算并输出运算结果。
1.先写一个分数类。
public class fractions {
public int fson;
public int fmom;
public int gcd(int a,int b) {
if (a < b) {
int c = a;
a = b;
b = c;
}
int r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
public void fAdd(int fson1,int fmom1,int fson2,int fmom2){
int fm=fmom1*fmom2;
fson1=fson1*fmom2;
fson2=fson2*fmom1;
int fs=fson1+fson2;
if( fs==0){
System.out.println("结果为0");
}
else {
int g = gcd(fs, fm);
System.out.println("最大公约数:" + g);
fs = fs / g;
fm = fm / g;
System.out.println("加法运算后结果中分子为:" + fs);
System.out.println("加法运算后结果中分母为:" + fm);
System.out.println("即分数为:" + fs + "/" + fm);
}
}
public void fSub(int fson1,int fmom1,int fson2,int fmom2){
int fm=fmom1*fmom2;
fson1=fson1*fmom2;
fson2=fson2*fmom1;
int fs=fson1-fson2;
if( fs==0){
System.out.println("结果为0");
}
else {
int g = gcd(fs, fm);
fs = fs / g;
fm = fm / g;
System.out.println("减法运算后结果中分子为:" + fs);
System.out.println("减法运算后结果中分母为:" + fm);
System.out.println("即分数为:" + fs + "/" + fm);
}
}
public void fMul(int fson1,int fmom1,int fson2,int fmom2){
int fm=fmom1*fmom2;
int fs=fson1*fson2;
if( fs==0){
System.out.println("结果为0");
}
else {
int g = gcd(fs, fm);
fs = fs / g;
fm = fm / g;
System.out.println("乘法运算后结果中分子为:" + fs);
System.out.println("乘法运算后结果中分母为:" + fm);
System.out.println("即分数为:" + fs + "/" + fm);
}
}
public void fDiv(int fson1,int fmom1,int fson2,int fmom2){
int fm=fmom1*fson2;
int fs=fson1+fmom2;
if( fs==0){
System.out.println("结果为0");
}
else {
int g = gcd(fs, fm);
fs = fs / g;
fm = fm / g;
System.out.println("除法运算后结果中分子为:" + fs);
System.out.println("除法运算后结果中分母为:" + fm);
System.out.println("即分数为:" + fs + "/" + fm);
}
}
}
2.调用类和方法。
import java.util.Scanner;
public class fractions2 {
public static void main(String[] args){
int i;
fractions fractions=new fractions();
Scanner in=new Scanner(System.in);
int fson1=0;
int fson2=0;
int fmom1=0;
int fmom2=0;
System.out.println("请输入第一个分数的分子:");
fson1=in.nextInt();
System.out.println("请输入第一个分数的分母:");
fmom1=in.nextInt();
System.out.println("请输入第二个分数的分子:");
fson2=in.nextInt();
System.out.println("请输入第二个分数的分母:");
fmom2=in.nextInt();
System.out.println("1.开始运算");
System.out.println("0.退出");
System.out.println("请输入选择:");
i=in.nextInt();
if(i==0){
System.out.println("已退出");
}
else {
while (i!=0) {
System.out.println("0.退出");
System.out.println("1.加法");
System.out.println("2.减法");
System.out.println("3.乘法");
System.out.println("4.除法");
System.out.println("请输入选择:");
i = in.nextInt();
switch (i) {
case 1:
fractions.fAdd(fson1, fmom1, fson2, fmom2);
break;
case 2:
fractions.fSub(fson1, fmom1, fson2, fmom2);
break;
case 3:
fractions.fMul(fson1, fmom1, fson2, fmom2);
break;
case 4:
fractions.fDiv(fson1, fmom1, fson2, fmom2);
break;
}
}
}
}
}
3.设计一个雇员类,创建雇员类对象,统计雇员的出勤人数
1.属性包括:编号、姓名、年龄、职务、部门、出勤人数
2.方法包括:构造方法、输出信息的方法、签到方法
要求:
1.考虑属性和方法的访问权限
2.考虑方法的功能
3.考虑main方法如何实现要求统计的信息
代码实现
1.Employee
import java.util.Scanner;
public class Employee {
Scanner in=new Scanner(System.in);
public String[] no=new String[100];
public String[] name=new String[100];
public String[] age=new String[100];
public String[] position=new String[100];
public String[] department=new String[100];
public int n=0;
public Employee(){
}
public void information(){
System.out.println("出勤人数:"+n);
for (int i=0;i<n;i++){
System.out.println("编号:"+no[i]);
System.out.println("姓名:"+name[i]);
System.out.println("年龄:"+age[i]);
System.out.println("职务:"+position[i]);
System.out.println("部门:"+department[i]);
}
}
public void sign(){
int i=0;
for (i=0;i<=100;i++){
System.out.println("请输入编号:");
no[i]=in.nextLine();
System.out.println("请输入名字:");
name[i]=in.nextLine();
System.out.println("请输入年龄:");
age[i]=in.nextLine();
System.out.println("请输入职务:");
position[i]=in.nextLine();
System.out.println("请输入部门:");
department[i]=in.nextLine();
System.out.println("如果你是最后一位签到请输入0,反之输入1");
int j=in.nextInt();
if (j==0){
break;
}
}
n=i+1;
}
}
2.Employee2
public class Employee2 {
public static void main(String[] args){
Employee employee=new Employee();
System.out.println("开始签到:");
employee.sign();
System.out.println("签到完毕!");
employee.information();
}
}
4.乒乓球运动员和篮球运动员,乒乓球教练和篮球教练。为了出国交流,跟乒乓球相关的人员都需要学习英语。
先创建一个包,在包中创建如下:
1.抽象类
Person抽象类
package jiekouchouxiang;
public abstract class Person {
private String name;
private int age;
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public abstract void eat();
public void sleep(){
System.out.println("睡觉要有足够的睡眠");
}
public void show(){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
}
Player抽象类
package jiekouchouxiang;
public abstract class Player extends Person {
public Player(){
super();
}
public Player(String name,int age){
super();
}
public abstract void student();
}
Coach抽象类
package jiekouchouxiang;
public abstract class Coach extends Person{
public Coach(){
super();
}
public Coach(String name,int age){
super();
}
public abstract void teacher();
}
2.接口
English接口
package jiekouchouxiang;
public interface English {
void speak();
}
3.具体类
PingpangPlayer
package jiekouchouxiang;
public class PingpangPlayer extends Player implements English {
@Override
public void student(){
System.out.println("乒乓球运动员要学习发球和接球技巧");
}
@Override
public void eat(){
System.out.println("乒乓球运动员要吃清淡的饭菜");
}
@Override
public void speak(){
System.out.println("乒乓球运动员要学习英语");
}
}
PingpangCoach
package jiekouchouxiang;
public class PingpangCoach extends Coach implements English {
@Override
public void teacher(){
System.out.println("乒乓球教练要教队员发球和接球技巧");
}
@Override
public void eat(){
System.out.println("乒乓球教练要吃清淡的饭菜");
}
@Override
public void speak(){
System.out.println("乒乓球教练要学习英语");
}
}
BasketballPlayer
package jiekouchouxiang;
public class BasketballPlayer extends Player {
@Override
public void student(){
System.out.println("篮球运动员要学习运球和投篮技巧");
}
@Override
public void eat(){
System.out.println("篮球运动员要吃高蛋白的饭菜");
}
}
BasketballCoach
package jiekouchouxiang;
public class BasketballCoach extends Coach {
@Override
public void teacher(){
System.out.println("篮球教练要教队员运球和投篮技巧");
}
@Override
public void eat(){
System.out.println("篮球教练要吃高蛋白的饭菜");
}
}
jiekouchouxiang
package jiekouchouxiang;
public class jiekouchouxiang {
public static void main(String[] args) {
PingpangPlayer ppp=new PingpangPlayer();
ppp.setName("马龙");
ppp.setAge(18);
ppp.show();
ppp.eat();
ppp.student();
ppp.speak();
System.out.println("*********************");
PingpangCoach ppc=new PingpangCoach();
ppc.setName("邓亚萍");
ppc.setAge(38);
ppc.show();
ppc.eat();
ppc.teacher();
ppc.speak();
System.out.println("*********************");
BasketballPlayer bp=new BasketballPlayer();
bp.setName("易建联");
bp.setAge(24);
bp.show();
bp.eat();
bp.student();
System.out.println("*********************");
BasketballCoach bc=new BasketballCoach();
bc.setName("姚明");
bc.setAge(35);
bc.show();
bc.eat();
bc.teacher();
System.out.println("*********************");
}
}
5.输入公里数,计算出所需的出租车费用
开封市的出租车计费方式为:起步2公里内5元,2公里上每公里收费1.3元,9公里以上每公里收费2元,燃油附加费1元。
import java.util.Scanner;
public class car {
public static void main(String[] args){
System.out.println("请输入公里数:");
Scanner scan= new Scanner(System.in);
double s=scan.nextDouble();
double sum;
if(s<=2){
sum=5+1;
System.out.println(s+"公里的费用"+sum+"元");
}
else if(s>2&&s<=9){
sum=5+(s-2)*1.3+1;
System.out.println(s+"公里的费用"+sum+"元");
}
else{
sum=5+(9-2)*1.3+(s-9)*2+1;
System.out.println(s+"公里的费用"+sum+"元");
}
}
}
再也不用担心被坑了!
6.用java来模拟抢手气红包,多线程
在上一个模拟微信抢红包的基础上,添加多线程
代码
import java.math.BigDecimal;
import java.util.Scanner;
public class qianhoubao {
public static void main(String[] args) {
System.out.println("-----模拟微信发拼手气红包-----");
Scanner sc = new Scanner(System.in);
System.out.println("请输入所发红包的总金额");
double money= sc.nextDouble();
System.out.println("请输入群成员个数");
int people = sc.nextInt();
String[] name = new String[people];
for (int i=0;i<people;i++){
System.out.println("请输入群成员名字");
name[i] = sc.next();
}
System.out.println("请输入所发红包的个数");
int num=sc.nextInt();
if (money / num == 0.01) {
for (int i = 0; i <people; i++) {
System.out.println(name[i] + "抢到红包红包0.01元");
}
} else if (money / num < 0.01) {
System.out.println("金额过小或个数过大,不合理!,请重新输入!");
} else {
Hongbao n=new Hongbao(num,money);
System.out.println("抢红包开始,红包总共"+money+"元");
for (int i = 0; i <people; i++) {
new Thread(new People(i, n,name)).start();
}
}
}
}
class Hongbao{
int num;
double money;
public Hongbao(int num,double money){
this.num=num;
this.money=new BigDecimal(money).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
}
synchronized public double qiang(){
double ramoney;
if(num>1){
ramoney=new BigDecimal(Math.random()*money).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
money-=ramoney;
num--;
}else if (num==1){
ramoney=new BigDecimal(money).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
money=0;
num--;
}else {
ramoney=0;
}
return ramoney;
}
}
class People implements Runnable{
int id;
Hongbao n;
String[] name;
double idmoney;
public People(int id,Hongbao n ,String[] name){
this.id=id;
this.n=n;
this.name=name;
}
@Override
public void run() {
System.out.println(name[id]+",开始抢红包");
try{
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
idmoney=n.qiang();
if(idmoney>0){
System.out.println("恭喜,"+name[id]+"抢到"+idmoney+"元");
}else {
System.out.println("很遗憾,"+name[id]+"手速慢了,未抢到红包");
}
}
}