JAVA课后习题作业答案2

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Exercise 2.30

import java.util.Scanner;

public class SeperateDigital//定义类SeperateDigital

{

public static void main( String args[] )

{

Scanner input = new Scanner( System.in );

int number; //定义输入数据的数据类型

System.out.println( "Enter one number consisting of five digits :" );

number = input.nextInt();//用户输入数据

System.out.println(

"\nThe digits separated from one another by three spaces each :" );

System.out.printf( number/10000+" " );//显示万位

System.out.printf( number/1000%10+"" );//显示千位

System.out.printf( number/100%10+"" );//显示百位

System.out.printf( number/10%10+"" );//显示十位

System.out.println( number%10+" \n" );//显示个位

}

}

Exercise 4.1

( a )

//从左上角画一组线段

import java.awt.Graphics;

import javax.swing.JPanel;

public class DrawPanel extends JPanel

{

public void paintComponent( Graphics g )

{

//引用paintComponent保证正确的显示线条

super.paintComponent( g );

int width = getWidth(); //总宽度

int height = getHeight(); //总高度

int widthstep = width/15;//横向每次移动的距离

int heightstep = height/15;//纵向每次移动的距离

//在给定的两点之间显示线段

for ( int n = 1;n <= 14;n++)

{

g.drawLine( 0, 0, widthstep*n, height - heightstep*n );

}//结束for循环

} //结束paintComponent

} //结束类DrawPanel

// Application to display DrawPanel.

import javax.swing.JFrame;

public class DrawPanelTest

{

public static void main( String args[] )

{

// create a panel that contains our drawing

DrawPanel panel = new DrawPanel();

// create a new frame to hold the panel

JFrame application = new JFrame();

// set the frame to exit when it is closed

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

application.add( panel ); // add the panel to the frame

application.setSize( 450 , 450 ); // set the size of the frame

application.setV isible( true ); // make the frame visible } // end main

} // end class DrawPanelTest

( b )

import java.awt.Graphics;

import javax.swing.JPanel;

public class DrawPanel3 extends JPanel

{

public void paintComponent( Graphics g )

{

//引用paintComponent保证正确显示线段

super.paintComponent( g );

int width = getWidth(); //总宽度

int height = getHeight(); //总高度

int widthstep = width/15; //横向每次移动的距离

int heightstep = height/15; //纵向每次移动的距离

//将宽、高均分成15等份,在给定的两点之间画线段

for ( int n = 1;n <= 14;n++ )

{

g.drawLine( 0, 0, widthstep*n , height - heightstep*n );

g.drawLine( width , height , widthstep*n, height - heightstep*n );

g.drawLine( width , 0 , widthstep*n , heightstep*n );

g.drawLine( 0 , height , widthstep*n , heightstep*n );

} //结束for循环

} // 结束方法paintComponent

} //结束类DrawPanelTest

import javax.swing.JFrame;

public class DrawPanel3Test

{

public static void main( String args[] )

{

// create a panel that contains our drawing

DrawPanel3 panel3 = new DrawPanel3();

// create a new frame to hold the panel

JFrame application = new JFrame();

// set the frame to exit when it is closed

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

application.add( panel3 ); // add the panel to the frame

application.setSize( 450 , 450 ); // set the size of the frame

application.setV isible( true ); // make the frame visible } // end main

} // end class DrawPanelTest

Exercise 4.2

( a )

import java.awt.Graphics;

import javax.swing.JPanel;

public class DrawLine1 extends JPanel

相关文档
最新文档