Java 進 階 程 式 設 計 03/14~03/21



Similar documents
System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

OBJECT ORIENTED PROGRAMMING IN JAVA EXERCISES

Chapter 2 Introduction to Java programming

Introduction to Java

Sample CSE8A midterm Multiple Choice (circle one)

Part I. Multiple Choice Questions (2 points each):

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Section 6 Spring 2013

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

J a v a Quiz (Unit 3, Test 0 Practice)

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS170 Lab 11 Abstract Data Types & Objects

Topic 11 Scanner object, conditional execution

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Building Java Programs

Line-based file processing

Introduction to Programming

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CISC 181 Project 3 Designing Classes for Bank Accounts

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

AP Computer Science Static Methods, Strings, User Input

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

JAVA ARRAY EXAMPLE PDF

Introduction to Java. CS 3: Computer Programming in Java

Week 1: Review of Java Programming Basics

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Building Java Programs

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Comp 248 Introduction to Programming

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

AP Computer Science File Input with Scanner

Lab 5: Bank Account. Defining objects & classes

More on Objects and Classes

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Building Java Programs

Limitations of Data Encapsulation and Abstract Data Types

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Design Patterns in Java

In this Chapter you ll learn:

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

Arrays in Java. Working with Arrays

Basics of Java Programming Input and the Scanner class

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

CSE 8B Midterm Fall 2015

AP Computer Science Java Subset

Programming in Java. What is in This Chapter? Chapter 1

JAVA - OBJECT & CLASSES

Some Scanner Class Methods

AP Computer Science Java Mr. Clausen Program 9A, 9B

Token vs Line Based Processing

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Introduction to Object-Oriented Programming

Java CPD (I) Frans Coenen Department of Computer Science

Object-Oriented Programming in Java

Homework/Program #5 Solutions

IBATIS - DYNAMIC SQL

(Eng. Hayam Reda Seireg) Sheet Java

Iteration CHAPTER 6. Topic Summary

Using Files as Input/Output in Java 5.0 Applications

Software Development with UML and Java 2 SDJ I2, Spring 2010

Using Two-Dimensional Arrays

2009 Tutorial (DB4O and Visual Studio 2008 Express)

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Programming Languages CIS 443

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

LAB4 Making Classes and Objects

Chapter 2 Elementary Programming

Java Basics: Data Types, Variables, and Loops

106 #3 EXAMPLE FOR SCOPE REGIONS void A() { int B; void E() { int C; { int D; #4 LARGE EXAMPLE FOR HAND SIMULATION class HS { int g1 = 100; int g2 = 2

LOOPS CHAPTER CHAPTER GOALS

CS 121 Intro to Programming:Java - Lecture 11 Announcements

B.Sc (Honours) - Software Development

Moving from CS 61A Scheme to CS 61B Java

Manual For Using the NetBeans IDE

Chapter 3. Input and output. 3.1 The System class

Course Intro Instructor Intro Java Intro, Continued

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Example of a Java program

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

CS114: Introduction to Java

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Lecture 1 Introduction to Java

Introduction to Java Lecture Notes. Ryan Dougherty

public void creditaccount(string accountnumber, float amount) { this.accounts.get(accountnumber).credit(amount); }

Transcription:

Java 進 階 程 式 設 計 03/14~03/21 Name: 邱 逸 夫 NO:942864 2.30 Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print 4 2 3 3 9 Assume that the user enters the correct number of digits. What happens when you execute the program and type a number with more than five digits? What happens when you execute the program and type a number with fewer than five digits? [Hint: It is possible to do this exercise with the techniques you learned in this chapter. You will need to use both division and remainder operations to "pick off" each digit.] c:\>java num2dig Enter an number:55663 5 5 6 6 3 c:\>java num2dig Enter an number:12 Number must in 5 digit! c:\>java num2dig Enter an number:989975 Number must in 5 digit! SteveHome Workstation HW List Page 1

E:\THUWorks\java 程 式 設 計 \num2dig.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name:number to digit 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 import java.util.scanner; // program uses Scanner 7 8 public class num2dig { 9 public static void main (String args[]) { 10 11 int input_value; 12 Scanner input = new Scanner( System.in ); 13 14 System.out.print("Enter an number:"); 15 input_value = input.nextint(); 16 17 if ((input_value > 99999) (input_value < 9999)) { 18 System.out.println("Number must in 5 digit!"); 19 } else { 20 //Do something... 21 22 int leader_digit = input_value/10000; 23 System.out.print(leader_digit); 24 System.out.print(" "); 25 input_value = input_value - (leader_digit * 10000); 26 27 int second_digit = input_value/1000; 28 System.out.print(second_digit); 29 System.out.print(" "); 30 input_value = input_value - (second_digit * 1000); 31 32 int third_digit = input_value/100; 33 System.out.print(third_digit); 34 System.out.print(" "); 35 input_value = input_value - (third_digit * 100); 36 37 int fouth_digit = input_value/10; 38 System.out.print(fouth_digit); 39 System.out.print(" "); 40 input_value = input_value - (fouth_digit * 10); 41 42 43 System.out.print(input_value); 44 System.out.println(); 45 } 46 47 } 48 } SteveHome Computing Workstation, Inc. Page 1

2.31 Using only the programming techniques you learned in this chapter, write an application that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format, as shown below. [Note: This program does not require any input from the user.] c:\>java shownum number square cube 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 SteveHome Workstation HW List Page 2

E:\THUWorks\java 程 式 設 計 \shownum.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name: 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 public class shownum { 7 8 public static void main (String args[]) { 9 10 System.out.println("number\tsquare\tcube"); 11 12 for (int i = 1; i <=10 ; i++) { 13 14 System.out.printf("%d\t%d\t%d\n", i, i*i, i*i*i); 15 16 } 17 18 } 19 } SteveHome Computing Workstation, Inc. Page 1

3.13 Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getinvoiceamount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoice's capabilities. c:\>java InvoiceTest Enter Number:5562 Enter Description:Some voice about you. Enter Quantity:6 Enter Price:39.9 The current item info: Number:5562 Description:Some voice about you. Quantity:6 Price:39.90 The current invoice amount: 239.39999999999998 SteveHome Workstation HW List Page 3

E:\THUWorks\java 程 式 設 計 \Invoice.java 2007 年 3 月 28 日 上 午 04:16 1 // Prog.Name:Invoice 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 public class Invoice { 7 8 private String part_number; 9 private String part_description; 10 private int part_quantity; 11 private double part_price; 12 13 public void setnumber (String number) { 14 part_number = number; 15 } 16 public void setdescription (String description) { 17 part_description = description; 18 } 19 public void setquantity (int quantity) { 20 if (quantity < 0) { 21 part_quantity = 0; 22 } else { 23 part_quantity = quantity; 24 } 25 } 26 public void setprice (double price) { 27 part_price = price; 28 } 29 30 public Invoice() { 31 } 32 33 public Invoice(String number, String description, int quantity, double price) { 34 setnumber(number); 35 setdescription(description); 36 setquantity(quantity); 37 setprice(price); 38 } 39 40 public String getnumber () { 41 return part_number; 42 } 43 public String getdescription () { 44 return part_description; 45 } 46 public int getquantity () { 47 return part_quantity; 48 } 49 public double getprice () { 50 return part_price; 51 } 52 public void showinvoice() { 53 System.out.printf("Number:%s\n", getnumber()); 54 System.out.printf("Description:%s\n", getdescription()); 55 System.out.printf("Quantity:%d\n", getquantity()); SteveHome Computing Workstation, Inc. Page 1

E:\THUWorks\java 程 式 設 計 \Invoice.java 2007 年 3 月 28 日 上 午 04:16 56 System.out.printf("Price:%.2f\n", getprice()); 57 } 58 public double getinvoiceamount() { 59 return part_quantity * part_price; 60 } 61 62 } SteveHome Computing Workstation, Inc. Page 2

E:\THUWorks\java 程 式 設 計 \InvoiceTest.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name:InvoiceTest (depend on Invoice) 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 import java.util.scanner; // program uses Scanner 7 8 public class InvoiceTest { 9 10 public static void main (String args[]) { 11 12 Scanner input = new Scanner( System.in ); 13 14 String part_number; 15 String part_description; 16 int part_quantity; 17 double part_price; 18 19 Invoice myinvoice = new Invoice(); 20 21 System.out.print("Enter Number:"); 22 // part_number = input.nextline(); 23 myinvoice.setnumber(input.nextline()); 24 25 System.out.print("Enter Description:"); 26 // part_description = input.nextline(); 27 myinvoice.setdescription(input.nextline()); 28 29 System.out.print("Enter Quantity:"); 30 // part_quantity = input.nextint(); 31 myinvoice.setquantity(input.nextint()); 32 33 System.out.print("Enter Price:"); 34 // part_price = input.nextint(); 35 myinvoice.setprice(input.nextdouble()); 36 37 System.out.println("The current item info:"); 38 myinvoice.showinvoice(); 39 40 System.out.println("The current invoice amount:"); 41 System.out.println(myInvoice.getInvoiceAmount()); 42 43 } 44 45 } SteveHome Computing Workstation, Inc. Page 1

3.14 Create a class called Employee that includes three pieces of information as instance variables a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again. E:\>java EmployeeTest Frist Employee Enter Frist Neme:Steve Enter Last Neme:Bob Enter Monthly Salary:0 Second Employee Enter Frist Neme:Steve Enter Last Neme:Joe Enter Monthly Salary:10 Third Employee Enter Frist Neme:Steve Enter Last Neme:Nick Enter Monthly Salary:19.9 This year is end. Our boss decided to give each employee's monthly salary +10%. Employee:Steve Bob Monthly Salary:0.0 The employee:steve Bob 's monthly salary NOW is 0.0 Employee:Steve Joe Monthly Salary:10.0 The employee:steve Joe 's monthly salary NOW is 11.0 Employee:Steve Nick Monthly Salary:19.9 The employee:steve Nick 's monthly salary NOW is 21.9 SteveHome Workstation HW List Page 4

E:\THUWorks\java 程 式 設 計 \Employee.java 2007 年 3 月 28 日 上 午 04:18 1 // Prog.Name:Employee 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/28/07 6 public class Employee { 7 8 private String first_name; 9 private String last_name; 10 private double monthly_salary; 11 12 public Employee () { 13 } 14 15 public Employee (String fname, String lname, double msalary) { 16 first_name = fname; 17 last_name = lname; 18 if (msalary < 0) { 19 monthly_salary = 0; 20 } else { 21 monthly_salary = msalary; 22 } 23 } 24 25 public void setfristname (String fname) { 26 first_name = fname; 27 } 28 29 public void setlastname (String lname) { 30 last_name = lname; 31 } 32 33 public void setmonthlysalary (double msalary) { 34 if (msalary < 0) { 35 monthly_salary = 0; 36 } else { 37 monthly_salary = msalary; 38 } 39 } 40 41 public String getfristname () { 42 return first_name; 43 } 44 public String getlastname () { 45 return last_name; 46 } 47 public double getmonthlysalary (){ 48 return monthly_salary; 49 } 50 51 public void showemployeeinfo() { 52 System.out.printf("Employee:%s %s\n", getfristname(), getlastname()); 53 System.out.printf("Monthly Salary:%.1f\n", getmonthlysalary()); 54 } 55 SteveHome Computing Workstation, Inc. Page 1

E:\THUWorks\java 程 式 設 計 \Employee.java 2007 年 3 月 28 日 上 午 04:18 56 } SteveHome Computing Workstation, Inc. Page 2

E:\THUWorks\java 程 式 設 計 \EmployeeTest.java 2007 年 3 月 28 日 上 午 04:18 1 import java.util.scanner; 2 3 public class EmployeeTest { 4 5 public static void main ( String args[] ){ 6 7 Scanner input = new Scanner( System.in ); 8 9 // Frist Employee 10 System.out.println("Frist Employee"); 11 Employee myfristemployee = new Employee(); 12 System.out.print("Enter Frist Neme:"); 13 myfristemployee.setfristname(input.nextline()); 14 System.out.print("Enter Last Neme:"); 15 myfristemployee.setlastname(input.nextline()); 16 System.out.print("Enter Monthly Salary:"); 17 myfristemployee.setmonthlysalary(input.nextdouble()); 18 19 // Second Employee 20 System.out.println("Second Employee"); 21 input.nextline(); //Drop next line to fix input error. 22 Employee mysecondemployee = new Employee(); 23 System.out.print("Enter Frist Neme:"); 24 mysecondemployee.setfristname(input.nextline()); 25 System.out.print("Enter Last Neme:"); 26 mysecondemployee.setlastname(input.nextline()); 27 System.out.print("Enter Monthly Salary:"); 28 mysecondemployee.setmonthlysalary(input.nextdouble()); 29 30 // Third Employee 31 System.out.println("Third Employee"); 32 input.nextline(); //Drop next line to fix input error. 33 Employee mythirdemployee = new Employee(); 34 System.out.print("Enter Frist Neme:"); 35 mythirdemployee.setfristname(input.nextline()); 36 System.out.print("Enter Last Neme:"); 37 mythirdemployee.setlastname(input.nextline()); 38 System.out.print("Enter Monthly Salary:"); 39 mythirdemployee.setmonthlysalary(input.nextdouble()); 40 41 System.out.println(); 42 System.out.println("This year is end. Our boss decided to give each employee's monthly salary +10%.\n"); 43 44 System.out.println(); 45 myfristemployee.showemployeeinfo(); //Show current Employee Info 46 myfristemployee.setmonthlysalary(myfristemployee.getmonthlysalary() * (11.0 /10.0)); //Add MonthlySalary to 110% 47 System.out.printf("The employee:%s %s 's monthly salary NOW is %.1f\n", myfristemployee. getfristname(), myfristemployee.getlastname(), myfristemployee.getmonthlysalary()); 48 49 System.out.println(); 50 mysecondemployee.showemployeeinfo(); //Show current Employee Info 51 mysecondemployee.setmonthlysalary(mysecondemployee.getmonthlysalary() * ( 11.0/10.0)); //Add MonthlySalary to 110% 52 System.out.printf("The employee:%s %s 's monthly salary NOW is %.1f\n", mysecondemployee. SteveHome Computing Workstation, Inc. Page 1

E:\THUWorks\java 程 式 設 計 \EmployeeTest.java 2007 年 3 月 28 日 上 午 04:18 getfristname(), mysecondemployee.getlastname(), mysecondemployee.getmonthlysalary ()); 53 54 System.out.println(); 55 mythirdemployee.showemployeeinfo(); //Show current Employee Info 56 mythirdemployee.setmonthlysalary(mythirdemployee.getmonthlysalary() * (11.0 /10.0)); //Add MonthlySalary to 110% 57 System.out.printf("The employee:%s %s 's monthly salary NOW is %.1f\n", mythirdemployee. getfristname(), mythirdemployee.getlastname(), mythirdemployee.getmonthlysalary()); 58 59 } 60 } SteveHome Computing Workstation, Inc. Page 2

3.15 Create a class called Date that includes three pieces of information as instance variables a month (type int), a day (type int) and a year (type int). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displaydate that displays the month, day and year separated by forward slashes (/). Write a test application named DateTest that demonstrates class Date's capabilities. E:\>java DateTest DateTest for Object Date. Enter year:1 Enter month:1 Enter day:1 Incorrect year, the data will rest to 1990! You just enter the date:1/1/1990 E:\>java DateTest DateTest for Object Date. Enter year:2000 Enter month:2 Enter day:29 You just enter the date:2/29/2000 E:\>java DateTest DateTest for Object Date. Enter year:2001 Enter month:2 Enter day:29 Incorrect day(february check), the data will rest to 1! You just enter the date:2/1/2001 E:\>java DateTest DateTest for Object Date. Enter year:2006 Enter month:7 Enter day:31 You just enter the date:7/31/2006 E:\>java DateTest DateTest for Object Date. Enter year:2006 Enter month:7 Enter day:31 SteveHome Workstation HW List Page 5 You just enter the date:7/31/2006

E:\THUWorks\java 程 式 設 計 \Date.java 2007 年 3 月 28 日 上 午 04:18 1 // Prog.Name:Date 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/28/07 6 public class Date { 7 private int month = 1; 8 private int day = 1; 9 private int year = 1990; //The Java's pre(oak) birthday. 10 private int replace_date = 0; // 0 = 28 days, 1 = 29 days for February use only. 11 private int replace_day = 1; // 0 = 30 days, 1 = 31 days 12 13 public void setyear(int the_year) { 14 if ((1989<the_year) && (65535>the_year)) { 15 year = the_year; 16 } else { 17 System.out.println("Incorrect year, the data will rest to 1990!"); 18 } 19 // 不 能 被 4 整 除 的 不 是 閏 年, 能 被 100 整 除 而 不 能 被 400 整 除 的 年 份 不 是 閏 年, 能 被 3200 整 除 的 也 不 是 閏 年 20 if ((the_year%4!= 0) ((the_year%100 == 0) && (the_year%400!= 0)) ( the_year%3200 == 0)) { 21 replace_date = 0; 22 } else { 23 replace_date = 1; 24 } 25 } 26 27 public void setmonth(int the_month) { 28 if ((0<the_month) && (13>the_month)) { 29 month = the_month; 30 } else { 31 System.out.println("Incorrect month, the data will rest to 1!"); 32 } 33 if (((the_month%2 == 1) && (the_month <= 7)) ((the_month%2 == 0) && ( the_month >= 8))) { //1 3 5 7 8 10 12 34 replace_day = 1; 35 } else { //2 4 6 8 9 11 36 replace_day = 0; 37 } 38 } 39 40 public void setday(int the_day) { 41 if (replace_day == 1) { 42 if ((0<the_day) && (32>the_day) ) { 43 day = the_day; 44 } else { 45 System.out.println("Incorrect day(even check), the data will rest to 1!"); 46 } 47 } else if (getmonth() == 2) { //28 天, 是 否 閏 年? 48 if ((0<the_day) && (30>the_day) && (replace_date == 1)) { 49 day = the_day; 50 } else if ((0<the_day) && (29>the_day) && (replace_date == 0)) { 51 day = the_day; 52 } else { 53 System.out.println("Incorrect day(february check), the data will rest to 1!"); SteveHome Computing Workstation, Inc. Page 1

E:\THUWorks\java 程 式 設 計 \Date.java 2007 年 3 月 28 日 上 午 04:18 54 } 55 } else { 56 if ((0<the_day) && (31>the_day)) { 57 day = the_day; 58 } else { 59 System.out.println("Incorrect day(odd check), the data will rest to 1!"); 60 } 61 } 62 } 63 64 public int getmonth(){ 65 return month; 66 } 67 public int getday(){ 68 return day; 69 } 70 public int getyear(){ 71 return year; 72 } 73 74 public Date (int the_month, int the_day, int the_year){ 75 setyear(the_year); 76 setmonth(the_month); 77 setday(the_day); 78 } 79 80 public String displaydate () { 81 //return year.tostring() + "/" + month.tostring()+ "/" + day.tostring(); 82 return month + "/" + day + "/" + year; 83 } 84 } SteveHome Computing Workstation, Inc. Page 2

E:\THUWorks\java 程 式 設 計 \DateTest.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name:DateTest 2 // StdNo.:s942864 邱 逸 夫 3 // input: year, month, day. All input value must be Type:int. 4 // output: see code. 5 // Date: 03/28/07 6 import java.util.scanner; // program uses Scanner 7 8 public class DateTest { 9 10 public static void main(string args[]) { 11 12 int month; 13 int day; 14 int year; 15 byte trymore = 'y'; 16 17 System.out.println("DateTest for Object Date."); 18 19 Scanner input = new Scanner( System.in ); 20 21 System.out.print("Enter year:"); 22 year = input.nextint(); 23 System.out.print("Enter month:"); 24 month = input.nextint(); 25 System.out.print("Enter day:"); 26 day = input.nextint(); 27 28 Date mydate = new Date(month, day, year); 29 30 System.out.println("You just enter the date:" + mydate.displaydate()); 31 32 33 34 } 35 36 } SteveHome Computing Workstation, Inc. Page 1