《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)。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)

}

}

、Applet为一种特殊的Panel,它可作为Java Applet程序的最外层容器。 ( )

2、Java的源码中定义多少个类,编译后结果就生成多少个以.class为后缀的字节码文 ( )

3、Java程序中,使用关键字new创建新的类对象,使用关键字free回收无用的类对象。 ( )

4、Java有垃圾回收机制,可在指定的时间使用内存回收程序释放内存对象。( )

5、构造函数用于创建类的实例对象,返回类型为void,构造函数名应与类名相同。 ( )

6、在异常处理中,假设try中的代码可能产生多种异常则可以对应多个catch语句,若catch中的参数类型有父子关系,此时应该将子类放在前面,父类放在后面。 ( )

7、含有abstract方法的类是抽象类,而抽象类中可以没有abstract方法。 ( )

8、Java的屏幕坐标以像素为单位,容器的左下角被确定为坐标的起点。 ( )

9、静态初始化器是在其所属的类加载内存时由系统自动调用执行。 ( )

10、在Java中可使用赋值号( )对对象赋值,相当于生成了一个各属性与赋值对象相同的新对象。 ( )

11、MVC是Model-View-Controller的简写。"View" 是应用的表示面(由JSP页面产生)。( )

12、MVC是Model-View-Controller的简写。"Controller" 是提供应用的处理过程控制(一般是一个Servlet),通过这种设计模型控制应用逻辑。( )

13、Tomcat是支持JSP引擎的一种服务器软件,可以通过server.xml文件修改服务器的配置信息。( )

14、通过Request.getParameterValues()可以获得某个参数的多个值( )

15、运行JSP需要有JSP引擎,Tomcat是支持JSP的Web服务器的一种。( )

16、Jsp使用的是Java脚本代码,可跨平台运行,运行开销小,具有速度快,可扩展性好及支持分布式处理等特点。 ( )

17、在定义了Bean的属性后,可不创建属性方法。( )

18、if语句是Java的条件分支语句,它控制程序以两条路径执行。( )

19、包是类的容器,用于保证类名空间的一致性。( )

20、JSP中不能使用HTML中的“<!-- -->”注释方式,只能使用JSP的“<!-- -->”的注释方式。

答案:仅供参考

T T F T T T T F T F T T T F FF T T T F

21.Java application中的主类必须包含方法main,以下哪项是main方法的正确参数?()

A、 String args B、String ar[] C、Char argD、StringBuffer args[]

22.以下对继承的正确描述是( )。

A、在Java中类只允许单一继承

B、在Java中一个类只能实现一个接口

C、在Java中一个类不能同时继承一个类和实现一个接口

D、在Java中接口只允许单一继承

23.paint()方法中的参数可使用哪种类型? ()

A、Graphics B、Graphics2D C、StringD、Color

24.以下不属于Java基本数据类型的是( )

A、int B、Boolean C、float D、char

25.以下哪项可能包含菜单条()。

A、PanelB、Frame C、AppletD、Dialog

26.应使用哪种修饰符定义一个类域或类方法?()

A、staticB、package C、private D、public

27.在浏览器中执行applet 程序,以下选项中的哪个方法将被最先执行( )。

A、init() B、start() C、destroy() D、stop()

28.参考以下代码,关于该程序以下哪个说法是正确的?()

public class Person{

static int arr[] = new int[3]

public static void main(String a[])

{

System.out.println(arr[0]) }

}

A、编译时将产生错误 B、编译时正确,运行时将产生错误

C、输出零 D、输出空

29.下列语句中,哪些关于Java内存回收的说明是正确的? ( )

A、程序员必须创建一个线程来释放内存

B、内存回收程序负责释放无用内存

C、内存回收程序允许程序员直接释放内存

D、内存回收程序可以在指定的时间释放内存对象

30.以下关键字中,可以用来对对象加互斥锁的是?( )

A、transient B、synchronized C、 serialize D、static

31.以下代码段执行后的输出结果为( )

int x=-3; int y=-10;

System.out.println(y%x)

A、 -1 B、2 C、1D、3

32.有以下程序片段,下列哪个选项不能插入到行1。( )

1.

2.public class Interesting{

3.//do sth

4. }

A、import java.awt.* B、package mypackage

C、class OtherClass{ } D、public class MyClass{ }

33.假设有如下两个赋值语句:

a = Integer.parseInt(“5”)

b = Integer.valueOf(“5”).intValue()

下述说法正确的是( )。

A、a是整数类型变量,b是整数类对象。 B、a是整数类对象,b是整数类型变量。

C、a和b都是整数类对象并且值相等。D、a和b都是整数类型变量并且值相等。


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

原文地址: https://outofmemory.cn/yw/11040095.html

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

发表评论

登录后才能评论

评论列表(0条)

保存