public class WuMingFen {
private String theMa; //面码
private int quantity;//粉的分量(两)
private boolean likeSoup;//是否带汤
public WuMingFen(){
thistheMa = "酸辣";
thisquantity = 2;
thislikeSoup = true;
}
public WuMingFen(String theMa,int quantity){
thistheMa = theMa;
thisquantity = quantity;
}
public WuMingFen(String theMa,int quantity,boolean likeSoup){
thistheMa = theMa;
thisquantity = quantity;
thislikeSoup = likeSoup;
}
public void check(){
Systemoutprintln("面码:"+thistheMa);
Systemoutprintln("粉的分量(两):"+thisquantity);
Systemoutprintln("是否带汤:"+thislikeSoup);
}
//验证
public static void main(String[] args){
WuMingFen f1 = new WuMingFen("牛肉",3,true);
WuMingFen f2 = new WuMingFen("牛肉",2);
WuMingFen f3 = new WuMingFen();
f1check();
f2check();
f3check();
}
}
===============================第一题==============================
import javaappletApplet;
import javaawtColor;
import javaawtLabel;
public class test extends Applet {
private Label label;
@Override
public void init() {
label=new Label("欢迎来到java世界!");
labelsetBackground(ColorBLUE);
setBackground(ColorPINK);
add(label);
}
}
===============================第二题==============================
因为没图,所以自己设计的界面
import javaawtBorderLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJScrollPane;
import javaxswingJTextArea;
public class test implements ActionListener {
private JFrame frame;
private JLabel label;
private JTextArea jt;
private JScrollPane jsp;
private JButton show;
public test() {
frame=new JFrame("Test");
label=new JLabel("显示内容");
jt=new JTextArea(10,20);
jsp=new JScrollPane(jt);
show = new JButton("显示给定内容");
JPanel panel=new JPanel();
paneladd(label);
paneladd(show);
frameadd(jsp,BorderLayoutNORTH);
frameadd(panel,BorderLayoutSOUTH);
showaddActionListener(this);
show();
}
public void show(){
framesetLocation(200, 200);
framesetSize(300, 260);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
labelsetText(jtgetText());
}
public static void main(String[] args) {
new test()show();
}
}
package localtea;
import localteaTeaFlavor;
public class InfuseMan extends TTobj {
public InfuseMan(String name) {
super(name);
}
public void takeTea(Flavor flavor, TeacupTray tt) {
ttsetTea(new Tea(flavor));
Systemoutprintln(name + " 沏了一杯 " + flavor );
TeaStallreleaseInfuseMan(this);
}
}
package localtea;
public class TTobj {
protected String name;
public TTobj(String name) {
thisname = thisgetClass()getSimpleName() + name;
}
}
package localtea;
public class Tea {
public static enum Flavor {
西湖龙井, 洞庭碧螺春, 白毫银针, 君山银针, 黄山毛峰, 武夷岩茶, 安溪铁观音, 信阳毛尖, 庐山云雾, 六安瓜片
}
private Flavor flavor;
public Tea(Flavor flavor) {
thissetFlavor(flavor);
}
public void setFlavor(Flavor flavor) {
thisflavor = flavor;
}
public Flavor getFlavor() {
return flavor;
}
}
package localtea;
public class TeaClient extends TTobj implements Runnable {
TeaFlavor favoriteFlavor = TeaFlavor六安瓜片;
public TeaClient(String string) {
super(string);
Systemoutprintln(thisname + " 大驾光临");
}
public TeaClient(String name, TeaFlavor flavor) {
this(name);
favoriteFlavor = flavor;
}
@Override
public void run() {
Tea tea = null;
Waiter waiter = null;
try {
Systemoutprintln(name + " 小二!沏杯茶!" + favoriteFlavor);
waiter = TeaStalltakeWaiter();
waitertakeOrder(favoriteFlavor);
tea = waitertakeTea();
} catch (InterruptedException e) {
Systemoutprintln(name + " 没人理我啊!我愤然离开!" + egetStackTrace());
return;
}
Systemoutprintln(name + " 品尝并称赞着 " + teagetFlavor());
if (waiter != null) {
waiterrelease();
}
Systemoutprintln(name + " 我走啦~");
}
}
package localtea;
public class TeaMenu {
Tea tea;
}
package localtea;
import javautilconcurrentArrayBlockingQueue;
import javautilconcurrentBlockingQueue;
public class TeaStall {
private static String name = "有家茶庄";
public static final int CLIENT_MAX_NUM = 5;
private static BlockingQueue<InfuseMan> infuseMen = new ArrayBlockingQueue<InfuseMan>(
1);
private static BlockingQueue<Waiter> waiters = new ArrayBlockingQueue<Waiter>(
1);
/
@param args
/
public static void main(String[] args) {
waitersadd(new Waiter("小二张"));
infuseMenadd(new InfuseMan("烹煮沏"));
Systemoutprintln(name + " 开门啦");
new Thread(new TeaClient("梁朝伟"))start();
new Thread(new TeaClient("刘嘉玲", TeaFlavor武夷岩茶))start();
Systemoutprintln(name + " 打烊啦");
}
public static Waiter takeWaiter() throws InterruptedException {
return waiterstake();
}
public static void releaseWaiter(Waiter waiter) {
waitersadd(waiter);
}
public static InfuseMan takeInfuseMan() throws InterruptedException {
return infuseMentake();
}
public static void releaseInfuseMan(InfuseMan im) {
infuseMenadd(im);
}
}
package localtea;
public class Waiter extends TTobj {
public Waiter(String name) {
super(name);
}
TeaFlavor order;
TeacupTray tt = new TeacupTray();
public void takeOrder(TeaFlavor order) throws InterruptedException {
thisorder = order;
Systemoutprintln(name + " 这位爷您点了 " + order);
TeaStalltakeInfuseMan()takeTea(thisorder, tt);
}
public Tea takeTea() {
Systemoutprintln(name + " 茶来啦 ");
return ttgetTea();
}
public void release() {
TeaStallreleaseWaiter(this);
}
}
class TeacupTray {
private Tea tea;
public Tea getTea() {
return tea;
}
public void setTea(Tea tea) {
thistea = tea;
}
}package localtea;
import localteaTeaFlavor;
public class InfuseMan extends TTobj {
public InfuseMan(String name) {
super(name);
}
public void takeTea(Flavor flavor, TeacupTray tt) {
ttsetTea(new Tea(flavor));
Systemoutprintln(name + " 沏了一杯 " + flavor );
TeaStallreleaseInfuseMan(this);
}
}
package localtea;
public class TTobj {
protected String name;
public TTobj(String name) {
thisname = thisgetClass()getSimpleName() + name;
}
}
package localtea;
public class Tea {
public static enum Flavor {
西湖龙井, 洞庭碧螺春, 白毫银针, 君山银针, 黄山毛峰, 武夷岩茶, 安溪铁观音, 信阳毛尖, 庐山云雾, 六安瓜片
}
private Flavor flavor;
public Tea(Flavor flavor) {
thissetFlavor(flavor);
}
public void setFlavor(Flavor flavor) {
thisflavor = flavor;
}
public Flavor getFlavor() {
return flavor;
}
}
package localtea;
public class TeaClient extends TTobj implements Runnable {
TeaFlavor favoriteFlavor = TeaFlavor六安瓜片;
public TeaClient(String string) {
super(string);
Systemoutprintln(thisname + " 大驾光临");
}
public TeaClient(String name, TeaFlavor flavor) {
this(name);
favoriteFlavor = flavor;
}
@Override
public void run() {
Tea tea = null;
Waiter waiter = null;
try {
Systemoutprintln(name + " 小二!沏杯茶!" + favoriteFlavor);
waiter = TeaStalltakeWaiter();
waitertakeOrder(favoriteFlavor);
tea = waitertakeTea();
} catch (InterruptedException e) {
Systemoutprintln(name + " 没人理我啊!我愤然离开!" + egetStackTrace());
return;
}
Systemoutprintln(name + " 品尝并称赞着 " + teagetFlavor());
if (waiter != null) {
waiterrelease();
}
Systemoutprintln(name + " 我走啦~");
}
}
package localtea;
public class TeaMenu {
Tea tea;
}
package localtea;
import javautilconcurrentArrayBlockingQueue;
import javautilconcurrentBlockingQueue;
public class TeaStall {
private static String name = "有家茶庄";
public static final int CLIENT_MAX_NUM = 5;
private static BlockingQueue<InfuseMan> infuseMen = new ArrayBlockingQueue<InfuseMan>(
1);
private static BlockingQueue<Waiter> waiters = new ArrayBlockingQueue<Waiter>(
1);
/
@param args
/
public static void main(String[] args) {
waitersadd(new Waiter("小二张"));
infuseMenadd(new InfuseMan("烹煮沏"));
Systemoutprintln(name + " 开门啦");
new Thread(new TeaClient("梁朝伟"))start();
new Thread(new TeaClient("刘嘉玲", TeaFlavor武夷岩茶))start();
Systemoutprintln(name + " 打烊啦");
}
public static Waiter takeWaiter() throws InterruptedException {
return waiterstake();
}
public static void releaseWaiter(Waiter waiter) {
waitersadd(waiter);
}
public static InfuseMan takeInfuseMan() throws InterruptedException {
return infuseMentake();
}
public static void releaseInfuseMan(InfuseMan im) {
infuseMenadd(im);
}
}
package localtea;
public class Waiter extends TTobj {
public Waiter(String name) {
super(name);
}
TeaFlavor order;
TeacupTray tt = new TeacupTray();
public void takeOrder(TeaFlavor order) throws InterruptedException {
thisorder = order;
Systemoutprintln(name + " 这位爷您点了 " + order);
TeaStalltakeInfuseMan()takeTea(thisorder, tt);
}
public Tea takeTea() {
Systemoutprintln(name + " 茶来啦 ");
return ttgetTea();
}
public void release() {
TeaStallreleaseWaiter(this);
}
}
class TeacupTray {
private Tea tea;
public Tea getTea() {
return tea;
}
public void setTea(Tea tea) {
thistea = tea;
}
}
=========================
运行结果:
有家茶庄 开门啦
TeaClient梁朝伟 大驾光临
TeaClient刘嘉玲 大驾光临
TeaClient梁朝伟 小二!沏杯茶!六安瓜片
Waiter小二张 这位爷您点了 六安瓜片
InfuseMan烹煮沏 沏了一杯 六安瓜片
Waiter小二张 茶来啦
TeaClient梁朝伟 品尝并称赞着 六安瓜片
TeaClient梁朝伟 我走啦~
有家茶庄 打烊啦
TeaClient刘嘉玲 小二!沏杯茶!武夷岩茶
Waiter小二张 这位爷您点了 武夷岩茶
InfuseMan烹煮沏 沏了一杯 武夷岩茶
Waiter小二张 茶来啦
TeaClient刘嘉玲 品尝并称赞着 武夷岩茶
TeaClient刘嘉玲 我走啦~
public class Test {
static int gmCount = 0;
static int gtCount = 0;
static int tmCount = 0;
static int ttCount = 0;
static int exit = 1;
static int type = 0;
static int ticketType = 0;
static int number = 0;
public static void main(String[] args) {
according();
while (exit!=0){
type = accordingIn("输入1个人、2团体");
if(type==2){
number = accordingIn("团体输入人数");
if(number == 0){
break;
}
}else if(type == 1) {
number = 1;
}
else {
break;
}
ticketType = accordingIn("输入1门票、2通票");
if(ticketType==0){
break;
}
sellTicket(type,ticketType,number);
}
Systemoutprintln("今日个人门票出售"+gmCount+"张,共计:"+gmCount80+"元,团体门票出售"+tmCount+"张,共计"+tmCount60+"元");
Systemoutprintln("今日个人通票出售"+gtCount+"张,共计:"+gtCount100+"元,团体通票出售"+ttCount+"张,共计"+ttCount80+"元");
}
static void according(){
Systemoutprintln("---------------------");
Systemoutprintln("------电子售票系统-----");
Systemoutprintln("----------------------");
Systemoutprintln("--------个人-----团体--");
Systemoutprintln("--门票---80-------60---");
Systemoutprintln("--通票---100------80---");
Systemoutprintln("----------------------");
Systemoutprintln("输入0退出系统");
}
static int accordingIn(String message){
Scanner scanner = new Scanner(Systemin);
Systemoutprint(message + ":");
return scannernextInt();
}
static void sellTicket(int type,int ticketType,int number){
//个人门票
if(type==1&&ticketType==1){
gmCount =+ number;
}
//个人通票
if(type==1&&ticketType==2){
gtCount =+ number;
}
//团体门票
if(type==2&&ticketType==1){
tmCount =+ number;
}
//团体通票
if(type==2&&ticketType==2){
ttCount =+ number;
}
}
}
public class WuMingFen {
private String theMa ;
private int quantity ;
private boolean likeSoup ;
public static void main(String[] args) {
WuMingFen f3 = new WuMingFen();
f3setLikeSoup(true);
f3setTheMa("酸辣面");
f3setQuantity(2);
check(f3);
}
private static void check(WuMingFen fen){
Systemoutprintln("这是:"+fengetTheMa());
Systemoutprintln("分量:"+fengetQuantity());
if(fenisLikeSoup()){
Systemoutprintln("带汤");
}else{
Systemoutprintln("不带");
}
}
public WuMingFen(String theMa, int quantity) {
thistheMa = theMa;
thisquantity = quantity;
}
public WuMingFen() {
}
public WuMingFen(String theMa, int quantity, boolean likeSoup) {
super();
thistheMa = theMa;
thisquantity = quantity;
thislikeSoup = likeSoup;
}
public String getTheMa() {
return theMa;
}
public void setTheMa(String theMa) {
thistheMa = theMa;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
thisquantity = quantity;
}
public boolean isLikeSoup() {
return likeSoup;
}
public void setLikeSoup(boolean likeSoup) {
thislikeSoup = likeSoup;
}
@Override
public String toString() {
return "WuMingFen [theMa=" + theMa + ", quantity=" + quantity
+ ", likeSoup=" + likeSoup + "]";
这个事件有两个对象人和水,人有4个方法分别是缓冲、过滤、加热和放糖,所以这4个方法继承人的类,要么分细点加热的这些需要什么工具又分类,不过我觉得没那必要了,你就写个人类接口(或抽象类),有4个抽象方法,再写个实现类来实现接口(或继承抽象类),然后写个水的抽象类,有属性写属性没有就空吧。还有你这4个方法应该有先后顺序的吧,这个应该你写没问题,这个类只能产生一个对象,你需要用单例模式来设计这个类。还有什么不明白的可以问我,希望能解决你的问题
书上的一些定义你应该看过了吧。刚在网上看到两个例子,感觉很贴切,贴上来给你看看,应该很好理解了。
例子1:
说明:下面的”你”不特指某人 不具人身攻击
你自己和日常生活接触到的都是对象啊
你的长相”继承”了 你老爸,老妈的一些性状
但你也有自己区别他们的特征啊
大家都知道你是人都知道你具有人所必有的器官
你自己可以使用这些器官的功能 其他人没通过你的允许那就不能使用啊
你器官功能是否正常 你不说一般人是不知道的 你”封装”了它们啊
你还可以去美容什么的或学点什么技术(比方说你可以学习防卫 当有输入--有人侵犯你,时
本不具备还手能力的你就可以进行相应的输出--自卫 )
把自己”重写”一下
还有你日常生活用品什么的 你不用所有都自己制造啊(原始人可能是) 拿来用就好了!
不过你对某项东西制造原理和结构了解之后 你可以改造它 来为你所用
例子2:
面向对象就是面向你老婆,你跟她说“去倒茶”,她就会把茶到好;你说“老婆衣服颜色=红”,她就自己去把红色衣服换上。当你老婆做饭时,她会产生一个“帮忙”事件,当然,你可以选择处理或者不处理这个事件:p。你可以让老婆生Baby,这就是继承;老婆不会告诉你她有多少钱,这就是封装;老婆高兴了一个样,不高兴一个样,这就是“多态”……
你老婆面向对象就是你老婆面向你
她跟你说“去倒茶”,你就会把茶到好;她说“老公香烟Enable=false”,你就会乖乖把香烟戒掉。当你给老婆做饭时,你会产生一个“帮忙”事件,当然,她可以选择处理或者不处理这个事件:p。你老婆要是不想生Baby,那你就没得被继承;你存私房钱,这就是封装;你外面泡妞对这个一个样,对那个一个样,这就是“多态”,老婆让你打扫家里卫生,你把所有房间垃圾都清理一遍,这叫循环遍历,找到的垃圾装到垃圾带里,这叫装箱,把垃圾丢进了楼下垃圾箱里,这叫垃圾清理,忽然发现你不小心把自己包私房钱的报纸也丢了进去,赶紧去垃圾堆翻出来,这叫拆箱,找了半天没找到,发现垃圾已经被清扫工给收走了,啥呆呆的站在垃圾箱旁边,这叫挂起 ……
以上就是关于java面向对象编程习题,急求答案,用MyEclipse编写 为“无名的粉”写一个类:class全部的内容,包括:java面向对象编程习题,急求答案,用MyEclipse编写 为“无名的粉”写一个类:class、java面向对象程序设计期末考试编程题!急!!!、经典面向对象题,用Java做,要详细点的。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)