2、教员要求账号使用面向对象写一个计算机类
import java.util.Scanner; public class MyMain { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入2个数字:"); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); Calculator cal = new Calculator(); System.out.println("请输入符号(+-*/):"); char world = scanner.next().charAt(0); switch (world) { case ‘+‘: int add = cal.add(num1, num2); System.out.println(add); break; case ‘-‘: int jian = cal.jian(num1, num2); System.out.println(jian); break; case ‘*‘: int cheng = cal.cheng(num1, num2); System.out.println(cheng); break; case ‘/‘: int chu = cal.chu(num1, num2); System.out.println(chu); break; default: System.out.println("输入有误!"); } }}
public class Calculator { // 计算两数相加之和 public int add(int leftNum, int rightNum) { return leftNum + rightNum; } public int jian(int leftNum, int rightNum) { return leftNum - rightNum; } public int cheng(int leftNum, int rightNum) { return leftNum * rightNum; } public int chu(int leftNum, int rightNum) { return leftNum / rightNum; }}
3、假设当前时间是。。。编写一个CurrentTime类,定义show()方法显示时间
public class CurrentTime { String time="2015年5月12日10点11分00秒"; public void show(){ System.out.println(time); }}
public class MyMain { public static void main(String[] args) { CurrentTime currentTime = new CurrentTime(); currentTime.show(); }}
4、改进第三题
public class CurrentTime2 { String CurTime="2015,5,12"; public void show(){ System.out.println(CurTime); }}
public class MyMain { public static void main(String[] args) { CurrentTime2 cur=new CurrentTime2(); cur.show(); }}
5、描述计算机,使用类的方式
public class Computer { String name[] = {"cpu", "主板", "显示器", "硬盘", "内存"}; public void showInfo() { for (int i = 0; i < name.length; i++) { System.out.println(name[i]); } }}
public class MyMain { public static void main(String[] args) { Computer computer = new Computer(); computer.showInfo(); }}
6、某公司开发新游戏,编写测试类,创建英雄对象等等。。。
public class Hero { String name="李晓霞",life="300"; public void show(){ System.out.println("我是英雄,我的基本信息如下:"); System.out.println("姓名:"+name+","+"生命值:"+life); }}
public class Monster { String name="小龟",type="潜水型"; public void show(){ System.out.println("我是怪物,我的基本信息如下:"); System.out.println("姓名:"+name+","+"类型:"+type); }}
public class Weapon { String name="死神镰刀",gong="12"; public void show(){ System.out.println("我是武器,我的基本信息如下:"); System.out.println("武器名:"+name+","+"攻击力:"+gong); }}
public class MyMain { public static void main(String[] args) { Hero hero = new Hero(); Monster monster = new Monster(); Weapon weapon = new Weapon(); hero.show(); monster.show(); weapon.show(); }}
原文地址:https://www.cnblogs.com/yunfeioliver/p/9083669.html
时间: 2024-10-11 16:22:59