Java语言程序设计 基础篇第六版 (Y.Daniel Liang )的,第15 16章的编程题答案

Java语言程序设计 基础篇第六版 (Y.Daniel Liang )的,第15 16章的编程题答案,第1张

(1)。public class TiaoSeBan extends JFrame {

JPanel panel1;

JPanel toppanel;

JPanel bottompanel;

JPanel colorLabpanel;

JPanel colorScrollBarpanel;

JLabel redLable;

JLabel greenLable;

JLabel blueLable;

JLabel showColorLable;

JScrollBar redScrollBar;

JScrollBar greenScrollBar;

JScrollBar blueScrollBar;

void init(){

panel1=new JPanel();

toppanel=new JPanel();

bottompanel=new JPanel();

colorLabpanel=new JPanel();

colorScrollBarpanel=new JPanel();

redLable=new JLabel("Red");

greenLable =new JLabel("Green");

blueLable=new JLabel("Blue");

showColorLable=new JLabel("Show Colors");

redScrollBar =new JScrollBar(JScrollBarHORIZONTAL , 0, 100,0,255);

greenScrollBar =new JScrollBar(JScrollBarHORIZONTAL , 0,100,0,255);

blueScrollBar =new JScrollBar(JScrollBarHORIZONTAL , 0, 100,0,255);

}

TiaoSeBan(){

super();

init();

setLayout(new BorderLayout());

add(toppanel,BorderLayoutCENTER);

add(bottompanel,BorderLayoutSOUTH);

showColorLablesetHorizontalAlignment(SwingConstantsCENTER);

toppanelsetLayout(new BorderLayout());

toppaneladd(showColorLable,BorderLayoutCENTER);

bottompanelsetLayout(new BorderLayout());

bottompaneladd(colorLabpanel,BorderLayoutWEST);

bottompaneladd(colorScrollBarpanel,BorderLayoutCENTER);

colorLabpanelsetLayout(new GridLayout(3, 1));

colorLabpaneladd(redLable);

colorLabpaneladd(greenLable);

colorLabpaneladd(blueLable);

colorScrollBarpanelsetLayout(new GridLayout(3, 1));

colorScrollBarpaneladd(redScrollBar);

colorScrollBarpaneladd(greenScrollBar);

colorScrollBarpaneladd(blueScrollBar);

redScrollBaraddAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable);

}

});

greenScrollBaraddAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable);

}

});

blueScrollBaraddAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable);

}

});

}

void reSetColor(JLabel label){

labelsetForeground(new Color(redScrollBargetValue(), greenScrollBargetValue(), blueScrollBargetValue()));

}

public static void main(String[] args) {

TiaoSeBan frame=new TiaoSeBan();

framesetTitle("tiaoseban");

framesetLocationRelativeTo(null);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetSize(200,200);

framesetVisible(true);

}

}

(2)public class jisuanq extends JApplet implements ActionListener {

private JTextField jtf = new JTextField(10);

private boolean newNumber = true;

private int result = 0;

private String op = "=";

public void init() {

JPanel p = new JPanel();

psetLayout(new BorderLayout());

JPanel westPanel = new JPanel();

westPanelsetLayout(new GridLayout(5, 0));

westPaneladd(new JButton(" "));

westPaneladd(new JButton("MC"));

westPaneladd(new JButton("MR"));

westPaneladd(new JButton("MS"));

westPaneladd(new JButton("M+"));

Panel centerPanel = new Panel();

centerPanelsetLayout(new BorderLayout());

Panel p1 = new Panel();

Panel p2 = new Panel();

p1setLayout(new FlowLayout(FlowLayoutRIGHT));

p1add(new JButton("Back"));

p1add(new JButton("CE"));

p1add(new JButton("C"));

p2setLayout(new GridLayout(4, 5));

JButton bt;

p2add(bt = new JButton("7"));

btaddActionListener(this);

p2add(bt = new JButton("8"));

btaddActionListener(this);

p2add(bt = new JButton("9"));

btaddActionListener(this);

p2add(bt = new JButton("/"));

btaddActionListener(this);

p2add(bt = new JButton("sqrt"));

btaddActionListener(this);

p2add(bt = new JButton("4"));

btaddActionListener(this);

p2add(bt = new JButton("5"));

btaddActionListener(this);

p2add(bt = new JButton("6"));

btaddActionListener(this);

p2add(bt = new JButton(""));

btaddActionListener(this);

p2add(bt = new JButton("%"));

btaddActionListener(this);

p2add(bt = new JButton("1"));

btaddActionListener(this);

p2add(bt = new JButton("2"));

btaddActionListener(this);

p2add(bt = new JButton("3"));

btaddActionListener(this);

p2add(bt = new JButton("-"));

btaddActionListener(this);

p2add(bt = new JButton("1/x"));

btaddActionListener(this);

p2add(bt = new JButton("0"));

btaddActionListener(this);

p2add(bt = new JButton("+/-"));

btaddActionListener(this);

p2add(bt = new JButton(""));

p2add(bt = new JButton("+"));

btaddActionListener(this);

p2add(bt = new JButton("="));

btaddActionListener(this);

centerPaneladd(p2, BorderLayoutCENTER);

centerPaneladd(p1, BorderLayoutNORTH);

padd(centerPanel, BorderLayoutCENTER);

padd(westPanel, BorderLayoutWEST);

getContentPane()setLayout(new BorderLayout());

getContentPane()add(p, BorderLayoutCENTER);

getContentPane()add(jtf, BorderLayoutNORTH);

}

public void actionPerformed(ActionEvent e) {

String actionCommand = egetActionCommand();

if ('0' <= actionCommandcharAt(0) &&

actionCommandcharAt(0) <= '9') {

if (newNumber) {

jtfsetText(actionCommand);

newNumber = false;

}

else {

jtfsetText(jtfgetText() + actionCommand);

}

}

else

if (newNumber) {

if (actionCommandequals("-")) {

jtfsetText("-");

newNumber = false;

}

else

op = actionCommand;

}

else {

execute();

op = actionCommand;

}

}

void execute() {

int number = new Integer(jtfgetText())intValue();

Systemoutprintln("number " + op);

switch (opcharAt(0)) {

case '+': result += number; break;

case '-': result -= number; break;

case '': result = number; break;

case '/': result /= number; break;

case '%': result %= number; break;

case '=': result = number;

}

Systemoutprintln("result "+result);

jtfsetText(new Integer(result)toString());

newNumber = true;

}

/This main method enables the applet to run as an application/

public static void main(String[] args) {

// Create a frame

JFrame frame = new JFrame("Exercise16_8");

// Create an instance of the applet

jisuanq applet = new jisuanq();

// Add the applet instance to the frame

framegetContentPane()add(applet, BorderLayoutCENTER);

// Invoke init() and start()

appletinit();

appletstart();

// Display the frame

framesetSize(300, 300);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetLocationRelativeTo(null);

framesetVisible(true);

}

}

第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。

问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。

第二题可以答一下:

package pillar;

public class Pillar { private Geometry buttom;

private double height;

public Pillar() {

// TODO Auto-generated constructor stub

}

public Pillar(Geometry button,double height){

thisbuttom = button;

thisheight = height;

}

public double getVolume(){

return thisbuttomgetArea()height;

}

public Geometry getButtom() {

return buttom;

}

public void setButtom(Geometry buttom) {

thisbuttom = buttom;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

thisheight = height;

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public interface Geometry { double getArea();

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public class Circle implements Geometry { private double r;

public Circle() {

// TODO Auto-generated constructor stub

}

public Circle(double r) {

thisr = r;

}

public double getArea() { return MathPIrr;

}

public double getR() {

return r;

}

public void setR(double r) {

thisr = r;

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public class Rectangle implements Geometry { private double width;

private double height;

public Rectangle() {

// TODO Auto-generated constructor stub

}

public Rectangle(double width, double height) {

thiswidth = width;

thisheight = height;

}

public double getArea() { return thiswidththisheight;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

thiswidth = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

thisheight = height;

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar;

public class TestPillar {

/ @param args

/

public static void main(String[] args) {

Circle c = new Circle(5);

Rectangle r = new Rectangle(3,4);

Pillar p1 = new Pillar(c,6);

Pillar p2 = new Pillar(r,6);

Systemoutprintln("圆的体积:"+p1getVolume()+"\t矩形的体积:"+p2getVolume());

}

}

以上就是关于Java语言程序设计 基础篇第六版 (Y.Daniel Liang )的,第15 16章的编程题答案全部的内容,包括:Java语言程序设计 基础篇第六版 (Y.Daniel Liang )的,第15 16章的编程题答案、JAVA语言程序设计两道练习题。谢谢!、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9268389.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存