习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。

Slides:



Advertisements
Similar presentations
1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
Advertisements

Chapter 2 - Introduction to Java Applications
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Introduction to Computers and Programming Lecture 7:
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
Review of CIS 120 Concepts: What you said you want….
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
8/2: Recursion About Scoping.java Recursion Program of the Day.
Object Oriented Programming (OOP) LAB # 3 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
AP Java 10/4/2016.
2.5 Another Java Application: Adding Integers
Chapter 4: Control Structures I
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
Constructor Laboratory /12/4.
AP Java 10/4/2016.
Chapter 4 - Control Structures: Part 1
Applet 2019/4/23.
Debugging Exercise 00 Try compiling the code samples.
JOptionPane class.
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

习 题 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 / ) % 10; digit2 = ( number % 1000 / ) % 10; digit3 = ( number % 1000 % 100 / ) % 10; digit4 = ( number % 1000 % 100 % ) % 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 * digit2 * 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

// 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; LAB2

/* 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