设计一个电视机类,成员变量包括电视机编号、生产厂家、品牌名称、大小、开关状态,并可以对其进行相应操作,如打开电视、提高/降低音量、更换频道等。
- import java.util.*;
- class Tv
- {
- String manufacturer; //制造商
- String brandname; //品牌
- float size;
- boolean a;
- Tv()
- {
- manufacturer="桂林";
- brandname="双重幻想";
- size=30.0f;
- a=false; //电源关闭状态
- }
- void SetNum() //自动获取编号
- {
- Calendar cal=Calendar.getInstance();
- System.out.print(cal.get(Calendar.YEAR));
- System.out.print(cal.get(Calendar.MONTH));
- System.out.print(cal.get(Calendar.DATE));
- System.out.print(cal.get(Calendar.HOUR_OF_DAY));
- System.out.print(cal.get(Calendar.MINUTE));
- System.out.print(cal.get(Calendar.SECOND));
- }
- void PrintTv() //产品描述
- {
- System.out.print("编号:");
- SetNum();
- System.out.print(" 生产厂家:"+manufacturer+
- " 品牌:"+brandname+" 大小:"+size+"英寸 ");
- if(a==false)
- System.out.println("电源状态:关");
- else
- System.out.println("电源状态:开");
- }
- void OpenTv() //打开电视
- {
- System.out.println("电视被打开");
- System.out.println("Go on!");
- }
- void CloseTv() //关闭电视
- {
- System.out.println("电视被关闭");
- System.out.println("按5退出");
- }
- void Menu() //菜单
- {
- System.out.println("---欢迎使用 双重幻想 牌电视机---");
- System.out.println("1.打开电视(输入数字1则开)");
- System.out.println("2.更换频道(输入数字0~100)");
- System.out.println("3.提高/降低音量(输入数字(1~15)则提高音量,输入(-15~-1)则降低音量)");
- System.out.println("4.关闭电视(输入数字0则关)");
- System.out.println("5.退出");
- }
- void Voice()
- { System.out.println("请调节音量:");
- Scanner a=new Scanner(System.in);
- int voice=a.nextInt(); //键盘输入
- System.out.print("voice");
- if(voice>0)
- for(int i=0;i<voice;i++)
- System.out.print("+");
- else
- for(int i=voice;i<0;i++)
- System.out.print("-");
- System.out.println();
- System.out.println("Go on!");
- }
- void ChangeChannel()
- { System.out.println("输入你要选择的频道:");
- Scanner a=new Scanner(System.in);
- int channel=a.nextInt(); //键盘输入
- System.out.println("更换至"+channel+"频道成功");
- System.out.println("Go on!");
- }
- }
- public class Test
- {
- public static void main(String args[])
- {
- Tv t=new Tv();
- t.PrintTv();
- t.Menu();
- int number;
- do{
- Scanner a=new Scanner(System.in);
- number=a.nextInt(); //键盘输入
- switch(number)
- {
- case 1:t.OpenTv();break;
- case 2:t.ChangeChannel();break;
- case 3:t.Voice();break;
- case 4:t.CloseTv();break;
- case 5:break;
- }
- }while(number!=5);
- }
- }