PowerPoint 演示文稿

advertisement
习
题
4.23 编写一个applet,读取一个矩形的边长,然
后用在paint方法中使用drawString方法画出用星
组成的空心矩形。程序应能画出边长从1到20的任何
矩形。
// Hollow.java
// Program prints a hollow square
import javax.swing.*;
import java.awt.Graphics;
public class Hollow extends JApplet {
int stars;
public void init()
{
String input;
// for user input
// read number from user as a string
input =
JOptionPane.showInputDialog( "Enter length of side:" );
// convert numbers from type String to type int
stars = Integer.parseInt( input );
if ( stars < 1 ) {
stars = 1;
JOptionPane.showMessageDialog(
null, "Using default value 1", "Error",
JOptionPane.ERROR_MESSAGE );
}
else if ( stars > 20 ) {
stars = 20;
JOptionPane.showMessageDialog(
null, "Using default value 20", "Error",
JOptionPane.ERROR_MESSAGE );
}
}
public void paint( Graphics g )
{
int x = 5, y = 70, i = 1, j = 1;
while ( i <= stars ) {
while ( j <= stars ) {
if ( i == 1 )
g.drawString( "*", x += 5, y );
else if ( i == stars )
g.drawString( "*", x += 5, y );
else if ( j == 1 )
g.drawString( "*", x += 5, y );
else if ( j == stars )
g.drawString( "*", x += 5, y );
else
g.drawString( " ", x += 5, y );
j++; }
j = 1;
i++;
y += 5;
x = 5;
}
//end of while
}//end of paint
}//end of Hollow
4.31 一家公司想通过电话传输数据(数据以4位的
整数形式传输)…… 程序必须能从文本区中读取
输入的4位数字,并用以下方式加密:每位数字加7
后模10,然后将第1位数与第3位数交换,第2位数与
第4位数交换。最后打印出加密后的数据。编写一个
独立的应用程序,接受一个加密的4位数,并把它解
密成原来的数字。
// Exercise 4.31 Part A Solution Encrypt.java
// Program encrypts a four digit number
import java.awt.*;
import javax.swing.JOptionPane;
public class Encrypt {
public static void main( String args[] )
{
int digit1, digit2, digit3, digit4;
String inputString;
// Enter four digit number to be encrypted
inputString =
JOptionPane.showInputDialog( "Enter a four digit number: " );
int number = Integer.parseInt( inputString );
digit1 = ( number / 1000 + 7 ) % 10;
digit2 = ( number % 1000 / 100 + 7 ) % 10;
digit3 = ( number % 1000 % 100 / 10 + 7 ) % 10;
digit4 = ( number % 1000 % 100 % 10 + 7 ) % 10;
int temp = digit1;
digit1 = digit3 * 1000;
digit3 = temp * 10;
temp = digit2;
digit2 = digit4 * 100;
digit4 = temp;
int encryptedNumber;
encryptedNumber = digit1 + digit2 + digit3+ digit4;
JOptionPane.showMessageDialog(
null, "Encrypted number is " + encryptedNumber,
"Encrytor", JOptionPane.INFORMATION_MESSAGE );
}
}
System.exit( 0 );
// Exercise 4.31 Part B Solution Decrypt.java
// Program decrypts a four digit number
import java.awt.*;
import javax.swing.JOptionPane;
public class Decrypt {
public static void main( String args[] )
{
String inputString;
// Enter four digit number to be decrypted
inputString =
JOptionPane.showInputDialog( "Enter a four digit
number: " );
int number = Integer.parseInt( inputString );
int digit1, digit2, digit3, digit4;
digit1 = number / 1000;
digit2 = number % 1000 / 100;
digit3 = number % 1000 % 100 / 10;
digit4 = number % 1000 % 100 % 10;
int temp = ( digit1 + 3 ) % 10;
digit1 = ( digit3 + 3 ) % 10;
digit3 = temp;
temp = ( digit2 + 3 ) % 10;
digit2 = ( digit4 + 3 ) % 10;
digit4 = temp;
int decryptedNumber = digit1 * 1000 + digit2 * 100
+ digit3 * 10 + digit4;
JOptionPane.showMessageDialog(
null, "Decrypted number is " + decryptedNumber,
"Encrytor", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 ); }
}
LAB1
// Exercise 4.12: Credit.java
// Program monitors accounts.
import java.awt.*;
import javax.swing.JOptionPane;
public class Credit {
// main method begins execution of Java application
public static void main( String args[] )
{
String inputString,
// user input
resultsString,
// result String
creditReport;
// credit status
int account,
// account number
charges,
// total charges
credits,
// total credits
creditLimit,
// allowed credit limit
balance;
// coutomer's balance
inputString = JOptionPane.showInputDialog(
"Enter account number: " );
/* write code to convert the input string to an integer
and store it in account */
/* write code to input the rest of the customer information
and convert the inputs to integers.
Use inputString to input each value. */
/* write code to compute the new balance */
/* write the code that will check if the new balance is
greater than the credit limit, and set the proper value
for creditStatusString */
resultsString =
creditReport + "\nCurrent balance is " + balance + ".";
JOptionPane.showMessageDialog( null, resultsString,
creditReport, OptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
} // end method main
LAB2
// Exercise 4.24: Palindrome.java Program tests for a palindrome
import java.awt.*;
import javax.swing.JOptionPane;
public class Palindrome {
// main method begins execution of Java application
public static void main( String args[] )
{ String resultString; // result String
int number,
// user input number
originalNumber, // stores original value in number for output
digit1,
// first digit
digit2,
// second digit
digit4,
// fourth digit
digit5,
// fifth digit
digits;
// number of digits in input
number = 0;
digits = 0;
/* Write code here that inputs a five-digit number.
Display an error message if the number is not five digits.
Loop until a valid input is received. */
/* Write code that separates the digits in
the five digit number. Use division to isolate the left-most
digit in the number, use modulus to remove that digit
from the number and repeat. Store the original value of
number in variable originalNumber before performing
calculations. */
/* Write code that determines whether the first and last
digits are identical and the second and fourth digits are
identical. Assign resultString a string indicating whether or
not the original string is a palindrome. */
/* Display whether or not the given number is a palindrome. */
System.exit( 0 );
} // end method main
} // end class Palindrome
Download