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

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

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

import java.awt.BorderLayout

import java.awt.FlowLayout

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.util.Calendar

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

import javax.swing.JTextField

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[time.get(Calendar.YEAR) % 12]

}

/**

* * 根据日期获取星座 *

* @param time *

* @return

*/

public static String date2Constellation(Calendar time) {

int month = time.get(Calendar.MONTH)

int day = time.get(Calendar.DAY_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("查看结果")

button.addActionListener(this)

panel1 = new JPanel()

panel1.setLayout(new FlowLayout(FlowLayout.CENTER))

panel1.add(label1)

panel1.add(year)

panel1.add(label2)

panel1.add(month)

panel1.add(label3)

panel1.add(day)

panel1.add(button)

frame.setLayout(new BorderLayout())

frame.add(panel1,BorderLayout.NORTH)

panel2 = new JPanel()

output = new JTextField("",40)

panel2.add(output,JPanel.CENTER_ALIGNMENT)

frame.add(panel2,BorderLayout.CENTER)

frame.setSize(500, 100)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.setVisible(true)

}

public void actionPerformed(ActionEvent e) {

output.setText("")

int y = Integer.parseInt(year.getText())

int m = Integer.parseInt(month.getText())

int d = Integer.parseInt(day.getText())

Calendar calendar = Calendar.getInstance()

calendar.set(Calendar.YEAR, y)

calendar.set(Calendar.MONTH, m)

calendar.set(Calendar.DAY_OF_MONTH, d)

String zodica = date2Zodica(calendar)

String constellation = date2Constellation(calendar)

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

output.setText(str)

}

//testcode

public static void main(String[] args) {

new Constellation()

}

}

:1.No permission ————has for anybodyto enter the building.(N M ET1988)

A.been given B.givenC.to give D.be giving

A。析语境可知,该空表示被给,应用被动语态,has后面应接been done。

2.U ntil now,his hom ework has not————finished.

A.being B.be C.been D.to be

C。语境逻辑制约家庭作业是被完成的,因此该空应用被动语态,has后面应使用been done的形式。点评:现在完成时被动语态结构为has/have been done,使用has been done时主语为单数,使用have been done时主语为复数。解题时应首先分析语境逻辑确定现在完成时被动语态,然后对照其结构公式确定答案。

3.The window————.Y ou need notwash it again.

A.is w ashed B.was washedC.has been washed D.will be washed析:

C。该句强调窗户已被清洗。

4.It seem that dinner ____for a long time.

A.is cooked B.has been cooked C.was cooked D had been cooked

B.表示晚饭已经准备好了。对现在有影响。

5。When my article ____.I'll come to see you .

A.is finished B is being finished C has been finished D will be finished

C 现在完成时被动语态代替将来完成时被动语态。

1. I'm sorry, sir. Your recorder isn't ready yet. It______in the factory.

A. is being repaired B. is repaired

C. has been repaired

D. hasn't repaired

本题并没有明确的时间状语,但从上文的意思看,“修理”这个动作正在进行;it指代上文提到的录音机,它和repair之间存在被动关系。因此应该使用现在进行时的被动语态。B、C两项是被动语态,但时态不对,因此,本题正确答案是A。

2. Some measures______to protect wildlife resources.

A. are taking B. are taken

C. are being taken

D. being taken

解此题时首先要注意到词组take measures to do sth.,由于题干中把宾语measures提前,应该使用被动语态;句子的意思是“正在采取一些措施去保护野生资源”。在表达现在进行时的被动语态的结构中,选项A、B、D均不完整,正确答案应是C。

3. —Have you moved into the new house?

—Not yet. The rooms______.

A. are being painted

B. are painting

C. are painted

D. are being painting

通过题干情景,我们不难看出,由于正在粉刷,因此还不能搬进新房子。分析选项,B项语态不对;C项时态不对;D项结构错误,所以正确答案应该是A。

4. A new building______at the other side of the street. Work started last year and the building is expected to open next year.

A. is built

B. is being built

C. has built

D. will be built

题干的后半部分告诉我们:“去年开工,预计明年竣工。”由此可知,大楼正在建设中,所以选项A、C、D时态均不对。正确答案应该是B。

5. It is well-known to us that a lot of rivers______by the factories.

A. are polluting

B. have been polluting

C. are being polluting

D. are being polluted

题干的意思是:“众所周知,许多河流正在被工厂污染。”说明了目前河流正在被污染的严重状况。A、B两项语态不对;C项结构错误,正确答案是D。

the man was being operated at that time .

at that time we all being showed how to swim.

the bridge was being repaired when we passed.

the road was being repaired we had to make a

while the supper was being prepared the light

at tha t time my classment was being showed how to use computers.

the book was being translated by him this time last year.

the question was been disgussed at the meeting yesterday afternoon.

过去进行时只找到例句,不好意思。

(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(JScrollBar.HORIZONTAL , 0, 100,0,255)

greenScrollBar =new JScrollBar(JScrollBar.HORIZONTAL , 0,100,0,255)

blueScrollBar =new JScrollBar(JScrollBar.HORIZONTAL , 0, 100,0,255)

}

TiaoSeBan(){

super()

init()

setLayout(new BorderLayout())

add(toppanel,BorderLayout.CENTER)

add(bottompanel,BorderLayout.SOUTH)

showColorLable.setHorizontalAlignment(SwingConstants.CENTER)

toppanel.setLayout(new BorderLayout())

toppanel.add(showColorLable,BorderLayout.CENTER)

bottompanel.setLayout(new BorderLayout())

bottompanel.add(colorLabpanel,BorderLayout.WEST)

bottompanel.add(colorScrollBarpanel,BorderLayout.CENTER)

colorLabpanel.setLayout(new GridLayout(3, 1))

colorLabpanel.add(redLable)

colorLabpanel.add(greenLable)

colorLabpanel.add(blueLable)

colorScrollBarpanel.setLayout(new GridLayout(3, 1))

colorScrollBarpanel.add(redScrollBar)

colorScrollBarpanel.add(greenScrollBar)

colorScrollBarpanel.add(blueScrollBar)

redScrollBar.addAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable)

}

})

greenScrollBar.addAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable)

}

})

blueScrollBar.addAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable)

}

})

}

void reSetColor(JLabel label){

label.setForeground(new Color(redScrollBar.getValue(), greenScrollBar.getValue(), blueScrollBar.getValue()))

}

public static void main(String[] args) {

TiaoSeBan frame=new TiaoSeBan()

frame.setTitle("tiaoseban")

frame.setLocationRelativeTo(null)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.setSize(200,200)

frame.setVisible(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()

p.setLayout(new BorderLayout())

JPanel westPanel = new JPanel()

westPanel.setLayout(new GridLayout(5, 0))

westPanel.add(new JButton(" "))

westPanel.add(new JButton("MC"))

westPanel.add(new JButton("MR"))

westPanel.add(new JButton("MS"))

westPanel.add(new JButton("M+"))

Panel centerPanel = new Panel()

centerPanel.setLayout(new BorderLayout())

Panel p1 = new Panel()

Panel p2 = new Panel()

p1.setLayout(new FlowLayout(FlowLayout.RIGHT))

p1.add(new JButton("Back"))

p1.add(new JButton("CE"))

p1.add(new JButton("C"))

p2.setLayout(new GridLayout(4, 5))

JButton bt

p2.add(bt = new JButton("7"))

bt.addActionListener(this)

p2.add(bt = new JButton("8"))

bt.addActionListener(this)

p2.add(bt = new JButton("9"))

bt.addActionListener(this)

p2.add(bt = new JButton("/"))

bt.addActionListener(this)

p2.add(bt = new JButton("sqrt"))

bt.addActionListener(this)

p2.add(bt = new JButton("4"))

bt.addActionListener(this)

p2.add(bt = new JButton("5"))

bt.addActionListener(this)

p2.add(bt = new JButton("6"))

bt.addActionListener(this)

p2.add(bt = new JButton("*"))

bt.addActionListener(this)

p2.add(bt = new JButton("%"))

bt.addActionListener(this)

p2.add(bt = new JButton("1"))

bt.addActionListener(this)

p2.add(bt = new JButton("2"))

bt.addActionListener(this)

p2.add(bt = new JButton("3"))

bt.addActionListener(this)

p2.add(bt = new JButton("-"))

bt.addActionListener(this)

p2.add(bt = new JButton("1/x"))

bt.addActionListener(this)

p2.add(bt = new JButton("0"))

bt.addActionListener(this)

p2.add(bt = new JButton("+/-"))

bt.addActionListener(this)

p2.add(bt = new JButton("."))

p2.add(bt = new JButton("+"))

bt.addActionListener(this)

p2.add(bt = new JButton("="))

bt.addActionListener(this)

centerPanel.add(p2, BorderLayout.CENTER)

centerPanel.add(p1, BorderLayout.NORTH)

p.add(centerPanel, BorderLayout.CENTER)

p.add(westPanel, BorderLayout.WEST)

getContentPane().setLayout(new BorderLayout())

getContentPane().add(p, BorderLayout.CENTER)

getContentPane().add(jtf, BorderLayout.NORTH)

}

public void actionPerformed(ActionEvent e) {

String actionCommand = e.getActionCommand()

if ('0' <= actionCommand.charAt(0) &&

actionCommand.charAt(0) <= '9') {

if (newNumber) {

jtf.setText(actionCommand)

newNumber = false

}

else {

jtf.setText(jtf.getText() + actionCommand)

}

}

else

if (newNumber) {

if (actionCommand.equals("-")) {

jtf.setText("-")

newNumber = false

}

else

op = actionCommand

}

else {

execute()

op = actionCommand

}

}

void execute() {

int number = new Integer(jtf.getText()).intValue()

System.out.println("number " + op)

switch (op.charAt(0)) {

case '+': result += numberbreak

case '-': result -= numberbreak

case '*': result *= numberbreak

case '/': result /= numberbreak

case '%': result %= numberbreak

case '=': result = number

}

System.out.println("result "+result)

jtf.setText(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

frame.getContentPane().add(applet, BorderLayout.CENTER)

// Invoke init() and start()

applet.init()

applet.start()

// Display the frame

frame.setSize(300, 300)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.setLocationRelativeTo(null)

frame.setVisible(true)

}

}


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

原文地址: http://outofmemory.cn/yw/11036370.html

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

发表评论

登录后才能评论

评论列表(0条)

保存