《Java语言程序设计基础篇》第六版的练习题和编程题答案

《Java语言程序设计基础篇》第六版的练习题和编程题答案,第1张

哥们我给你写完了,耽误了我半个小时的时间啊!你直接运行就可以了

import javaawtBorderLayout;

import javaawtFlowLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javautilCalendar;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJPanel;

import javaxswingJTextField;

public class Constellation implements ActionListener{

private JFrame frame = null;

private JTextField year = null;

private JTextField month = null;

private JTextField day = null;

private JLabel label1 = null;

private JLabel label2 = null;

private JLabel label3 = null;

private JPanel panel1 = null;

private JPanel panel2 = null;

private JButton button = null;

private JTextField output = null;

public static final String[] zodiacArr = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇",

"马", "羊" };

public static final String[] constellationArr = { "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座",

"狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };

public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22,

22 };

/

根据日期获取生肖

@return 11

/

public static String date2Zodica(Calendar time) {

return zodiacArr[timeget(CalendarYEAR) % 12];

}

/

根据日期获取星座

@param time

@return

/

public static String date2Constellation(Calendar time) {

int month = timeget(CalendarMONTH);

int day = timeget(CalendarDAY_OF_MONTH);

if (day < constellationEdgeDay[month]) {

month = month - 1;

}

if (month >= 0) {

return constellationArr[month];

}

// default to return 魔羯

return constellationArr[11];

}

public Constellation(){

frame = new JFrame("计算生肖,星座");

year = new JTextField("",3);

month = new JTextField("",3);

day = new JTextField("",3);

label1 = new JLabel("请输入年份:");

label2 = new JLabel(",请输入月份:");

label3 = new JLabel(",请输入日期:");

button = new JButton("查看结果");

buttonaddActionListener(this);

panel1 = new JPanel();

panel1setLayout(new FlowLayout(FlowLayoutCENTER));

panel1add(label1);

panel1add(year);

panel1add(label2);

panel1add(month);

panel1add(label3);

panel1add(day);

panel1add(button);

framesetLayout(new BorderLayout());

frameadd(panel1,BorderLayoutNORTH);

panel2 = new JPanel();

output = new JTextField("",40);

panel2add(output,JPanelCENTER_ALIGNMENT);

frameadd(panel2,BorderLayoutCENTER);

framesetSize(500, 100);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetVisible(true);

}

public void actionPerformed(ActionEvent e) {

outputsetText("");

int y = IntegerparseInt(yeargetText());

int m = IntegerparseInt(monthgetText());

int d = IntegerparseInt(daygetText());

Calendar calendar = CalendargetInstance();

calendarset(CalendarYEAR, y);

calendarset(CalendarMONTH, m);

calendarset(CalendarDAY_OF_MONTH, d);

String zodica = date2Zodica(calendar);

String constellation = date2Constellation(calendar);

String str = "您输入的日期为:"+y+"年-"+m+"-月"+d+"日,得到的生肖:"+zodica+",星座:"+constellation;

outputsetText(str);

}

//testcode

public static void main(String[] args) {

new Constellation();

}

}

(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);

}

}

import javaawtFlowLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJTextField;

public class A implements ActionListener {

JFrame jf;

JLabel jl1;

JLabel jl2;

JLabel jl3;

JButton jb1;

JTextField jtf1;

JTextField jtf2;

JTextField jtf3;

public A(){

init();

showMe();

}

public void init(){

jf=new JFrame("拆分小数点");

jfsetLayout(new FlowLayout());

jl1=new JLabel("请输入小数");

jl2=new JLabel("整数为");

jl3=new JLabel("小数为");

jb1=new JButton("拆分");

jb1addActionListener(this);

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf2setEditable(false);

jtf3=new JTextField(10);

jtf3setEditable(false);

jfadd(jl1);

jfadd(jtf1);

jfadd(jl2);

jfadd(jtf2);

jfadd(jl3);

jfadd(jtf3);

jfadd(jb1);

}

public void showMe(){

jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

jfsetVisible(true);

jfsetLocation(300, 300);

jfpack();

}

@Override

public void actionPerformed(ActionEvent e) {

String comm=egetActionCommand();

if("拆分"equals(comm)){

String s=jtf1getText();

Systemoutprintln(s);

if(scontains("")){

String[] ss=ssplit("\\");

for (int i = 0; i < sslength; i++) {

Systemoutprintln(ss[i]);

}

jtf2setText(ss[0]);

jtf3setText(ss[1]);

}

}

}

public static void main(String[] args) {

new A();

}

}

import javaawtFlowLayout;

import javaawtGridLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javautilArrays;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJPanel;

import javaxswingJTextField;

public class B implements ActionListener {

public JFrame jf;

private JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8,jl9,jl10,jl11,jl12;

private JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6,jtf7,jtf8,jtf9,jtf10,jtf11,jtf12;

private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8,jp9,jp10,jp11,jp12,jp13;

private JButton jb;

public B(){

init();

showMe();

}

public void showMe(){

jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

jfsetVisible(true);

jfsetLocation(300, 300);

jfpack();

}

public void init(){

jl1=new JLabel("输入第1个数字");

jl2=new JLabel("输入第2个数字");

jl3=new JLabel("输入第3个数字");

jl4=new JLabel("输入第4个数字");

jl5=new JLabel("输入第5个数字");

jl6=new JLabel("输入第6个数字");

jl7=new JLabel("输入第7个数字");

jl8=new JLabel("输入第8个数字");

jl9=new JLabel("输入第9个数字");

jl10=new JLabel("输入第10个数字");

jl11=new JLabel("最大值为");

jl12=new JLabel("最小值为");

jl11setEnabled(false);

jl12setEnabled(false);

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf3=new JTextField(10);

jtf4=new JTextField(10);

jtf5=new JTextField(10);

jtf6=new JTextField(10);

jtf7=new JTextField(10);

jtf8=new JTextField(10);

jtf9=new JTextField(10);

jtf10=new JTextField(10);

jtf11=new JTextField(10);

jtf11setEditable(false);

jtf12=new JTextField(10);

jtf12setEditable(false);

jp1=new JPanel();

jp2=new JPanel();

jp3=new JPanel();

jp4=new JPanel();

jp5=new JPanel();

jp6=new JPanel();

jp7=new JPanel();

jp8=new JPanel();

jp9=new JPanel();

jp10=new JPanel();

jp11=new JPanel();

jp12=new JPanel();

jp13=new JPanel();

jb=new JButton("确定");

jbaddActionListener(this);

jp1setLayout(new FlowLayout());

jp1add(jl1);

jp1add(jtf1);

jp2setLayout(new FlowLayout());

jp2add(jl2);

jp2add(jtf2);

jp3setLayout(new FlowLayout());

jp3add(jl3);

jp3add(jtf3);

jp4setLayout(new FlowLayout());

jp4add(jl4);

jp4add(jtf4);

jp5setLayout(new FlowLayout());

jp5add(jl5);

jp5add(jtf5);

jp6setLayout(new FlowLayout());

jp6add(jl6);

jp6add(jtf6);

jp7setLayout(new FlowLayout());

jp7add(jl7);

jp7add(jtf7);

jp8setLayout(new FlowLayout());

jp8add(jl8);

jp8add(jtf8);

jp9setLayout(new FlowLayout());

jp9add(jl9);

jp9add(jtf9);

jp10setLayout(new FlowLayout());

jp10add(jl10);

jp10add(jtf10);

jp11setLayout(new FlowLayout());

jp11add(jl11);

jp11add(jtf11);

jp12setLayout(new FlowLayout());

jp12add(jl12);

jp12add(jtf12);

jf=new JFrame("最大值最小值");

jfsetLayout(new GridLayout(13, 1));

jfadd(jp1);

jfadd(jp2);

jfadd(jp3);

jfadd(jp4);

jfadd(jp5);

jfadd(jp6);

jfadd(jp7);

jfadd(jp8);

jfadd(jp9);

jfadd(jp10);

jfadd(jp11);

jfadd(jp12);

jfadd(jb);

}

@Override

public void actionPerformed(ActionEvent e) {

String comm=egetActionCommand();

if("确定"equals(comm)){

int i1=IntegerparseInt(jtf1getText());

int i2=IntegerparseInt(jtf2getText());

int i3=IntegerparseInt(jtf3getText());

int i4=IntegerparseInt(jtf4getText());

int i5=IntegerparseInt(jtf5getText());

int i6=IntegerparseInt(jtf6getText());

int i7=IntegerparseInt(jtf7getText());

int i8=IntegerparseInt(jtf8getText());

int i9=IntegerparseInt(jtf9getText());

int i10=IntegerparseInt(jtf10getText());

int arr[]={i1,i2,i3,i4,i5,i6,i7,i8,i9,i10};

Arrayssort(arr);

int max=arr[9];

int min=arr[0];

jtf11setText(max+"");

jtf12setText(min+"");

}

}

public static void main(String[] args) {

new B();

}

}

import javaawtFlowLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javautilArrays;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJTextField;

public class C implements ActionListener {

private JFrame jf;

private JLabel jl1,jl2;

private JTextField jtf1,jtf2;

JButton jb1;

public C(){

init();

showMe();

}

public void init(){

jf=new JFrame("字母排序");

jfsetLayout(new FlowLayout());

jl1=new JLabel("请输入字母");

jl2=new JLabel("排序后");

jb1=new JButton("排序");

jb1addActionListener(this);

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf2setEditable(false);

jfadd(jl1);

jfadd(jtf1);

jfadd(jl2);

jfadd(jtf2);

jfadd(jb1);

}

public void showMe(){

jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

jfsetVisible(true);

jfsetLocation(300, 300);

jfpack();

}

@Override

public void actionPerformed(ActionEvent e) {

String comm=egetActionCommand();

if("排序"equals(comm)){

String s=jtf1getText();

char[] c=new char[slength()];

if(sendsWith("#")){

for (int i = 0; i < slength(); i++) {

c[i]=scharAt(i);

}

}

Arrayssort(c);

String ss="";

for (int i = 0; i < clength; i++) {

ss=ss+c[i];

}

jtf2setText(sssubstring(1));

}

}

public static void main(String[] args) {

new C();

}

}

都是有界面的,写界面不难 就是很麻烦。。。我自己写的 都是可以直接运行的,有什么问题可以再问我。。。

1、B;2、C;3、B;4、B;5、B;6、C;7、B;8、C;9、C;10、A;11、D;12、D;13、B;14、A;15、D

无聊哦~~~

三楼说的不错!方法重载是指参数的个数与类型不一样!所以13题应该是A,不过1题就是看不准那个下划线或减号了,呜呜~~

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

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

原文地址: http://outofmemory.cn/zz/10052625.html

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

发表评论

登录后才能评论

评论列表(0条)

保存