1. Trang chủ
  2. » Tất cả

Lab 04

16 4 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 16
Dung lượng 911,55 KB

Nội dung

Lab Guide Lab version:1.0.0 Last updated:12/31/2013 Page Overview In this lab, you will write simple applications to practice creating classes and packages in Java You have to remember all basic features of Orient Object Programming and apply it into these exercises Objectives Once you have completed this lab, you will understand:  How to create a class  How to create fields and methods for a class  How to use access modifier  How to create a package  How to use classes in other package Exercises This Hands-On Lab is comprised by the following exercises:  Booking Room  Project Assignment Estimated time to complete this lab: hours Page Index EXERCISE 1: BOOKING ROOM Task - Create BookingRoom Project Task - Create Date Class Task - Create Time Class Task - Create BookingRoomClass Task - Write a Program class to accept and display information Task - Execute your program EXERCISE 2: 10 PROJECT ASSIGNMENT 10 Task - Create School Project 11 Task - Create people package 11 Task - Create Teacher class (people package) 11 Task - Write code for Teacher class 12 Task - Create Student class 13 Task - Write code for Student class 13 Task - Write Program Class in a different package, use classes in other package 15 Task - Execute your program 16 Page Next Step Exercise 1: Booking Room In this exercise you will write a program to allow user books rooms in a hotel This program will accept all information about rooms such as: check in date, check in time, number of rooms, room type and so on, and display this information to the user You have to create:  BookingRoom Class to accept and display the information about booking room  Date and Time Class to store information about check in date and check in time  A program to demonstrate using above classes to accept and display information Class diagram of the program: Task - Create BookingRoom Project Choose "File" menu ⇒ "New" ⇒ "Java project" The "New Java Project" dialog pops up  In the "Project name" field, enter " BookingRoom"  Check "Use default location"  In the "JRE" box, select your JDK version installed on your system  Click "Finish" Page Task - Create Date Class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up  In "Name" field, enter "Date "  Click "Finish" Enter the following codes for Date Class public class Date { public Date() { day = 0; month = 0; year = 0; } byte day; byte month; short year; } Task - Create Time Class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up  In "Name" field, enter "Time "  Click "Finish" Enter the following codes for Time Class: package OOP.Book; public class Time { public Time() { hours = 0; minutes = 0; } Page byte hours; byte minutes; } Task - Create BookingRoomClass In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up  In "Name" field, enter "Time "  Click "Finish" Write a constructor method: public BookingRoom() { CheckInDate = new Date(); CheckInTime = new Time(); } Variables for storing information about a booking Date CheckInDate; Time CheckInTime; byte noOfRooms; double totalPrice; byte typeOfRoom; Method to display the date stored in instance variable booking date void getBookingDate() { System.out.println("Check in Date: " + CheckInDate.day + "/" + CheckInDate.month + "/" + CheckInDate.year); } Method to set instance variable booking date void setBookingDate(byte d, byte m, short y) { CheckInDate.day = d; CheckInDate.month = m; CheckInDate.year = y; } Page Method to display time stored in instance variable check in Time void getBookingTime() { System.out.println("Check in Time: " + CheckInTime.hours + ":" + CheckInTime.minutes); } Method to set the instance variable check in time void setCheckInTime(Time obj) { CheckInTime.hours = obj.hours; CheckInTime.minutes = obj.minutes; } Method to compute the total price of tickets booked double getTotalPrice() { switch(typeOfRoom) { case 1: totalPrice = noOfRooms * 1000; break; case 2: totalPrice = noOfRooms * 1500; break; case 3: totalPrice = noOfRooms * 1600; break; } return totalPrice; } 10 Method to display all the information about the tickets booked void displayTicketInformation() { System.out.println("\nFollowing is the information about the room(s) booked: "); System.out.println(" -"); getBookingDate(); getBookingTime(); System.out.println("Number of tickets: " + noOfRooms); System.out.print("Type of Room: "); Page switch(typeOfRoom) { case 1: System.out.print("Single"); break; case 2: System.out.print("Double"); break; case 3: System.out.print("Twin"); break; } System.out.printf("\nTotal cost of the ticket: $%.2f", getTotalPrice()); } } Task - Write a Program class to accept and display information Import the Scanner Library import java.util.Scanner; Create a main() method for executing the program public static void main(String[] args) { } Code inside main() method:  Create an instance of BookingRoom class BookingRoom objBookingRoom = new BookingRoom();  Accept date of check in date in this hotel Scanner input = new Scanner(System.in); System.out.println("Enter check in date (mm dd yyyy): "); byte day = input.nextByte(); byte month = input.nextByte(); short year = input.nextShort();  Set the check in date by passing primitive values to a method Page objBookingRoom.setBookingDate(day, month, year);  Create an instance of Time class and store check in time in it Time objTime = new Time();  Accept time of check in date in this hotel System.out.println("Enter check in time (hh mm): "); objTime.hours = input.nextByte(); objTime.minutes = input.nextByte();  Set the checkin time by passing an object to a method objBookingRoom.setCheckInTime(objTime);  Accept and set the type of room System.out.println("Enter the type of room: \n1 Single\n2 Double\n3 Twin\n"); objBookingRoom.typeOfRoom = input.nextByte();  Accept and set the number of tickets System.out.println("Enter the number of rooms: "); objBookingRoom.noOfRooms = input.nextByte();  Display the ticket information objBookingRoom.displayTicketInformation(); Page Task - Execute your program To run the program, right-click anywhere on the source file "program.java" (or from the "Run" menu) ⇒ Choose "Run As" ⇒ "Java Application" The output appears on the "Console" panel Exercise 2: Project assignment In this exercise, you will write a program to help an Administrator in a school accept information about student (name, age, address, project which they want to do, etc) and teacher (code, name, address, etc) This program also allow assign a student with a teacher who he/she want to work with Class diagram are shown below: Page 10 Task - Create School Project Choose "File" menu ⇒ "New" ⇒ "Java project" The "New Java Project" dialog pops up  In the "Project name" field, enter "School"  Check "Use default location"  In the "JRE" box, select your JDK version installed on your system  Click "Finish" Task - Create people package In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ package The "New Java Package" dialog pops up  In "Name" field, enter "people "  Click "Finish" Task - Create Teacher class (people package) In the "Package Explorer" (left panel) ⇒ Right-click on your people package ⇒ New ⇒ Class The "Ne w Java Class" dialog pops up  In "Name" field, enter "Teacher " Page 11  Click "Finish" Task - Write code for Teacher class Constructor public Teacher() { code = 0; name = ""; address = ""; phone = ""; } Instance variable to store teacher information private int code; /** Instance variable to store teacher name */ private String name; /** Instance variable to store the address of the teacher */ private String address; /** Instance variable to store phone number of the teacher */ private String phone; Method to retrieve the teacher name public String getName() { return name; } Method to retrieve the teacher code public int getCode() { return code; } Method to display the teacher information public void getTeacherInfo() { System.out.println("The details about the teacher are:"); System.out.println("Code: " + code); System.out.println("Address: " + address); System.out.println("Phone Number: " + phone); } Page 12 Method to accept the details about a teacher public void setTeacherInfo() { Scanner input = new Scanner(System.in); System.out.println("Enter details about the teacher:"); System.out.println("Enter teacher's code:"); code = input.nextInt(); input.nextLine(); System.out.println("Enter name:"); name = input.nextLine(); System.out.println("Enter address:"); address = input.nextLine(); System.out.println("Enter phone number:"); phone = input.nextLine(); } Task - Create Student class In the "Package Explorer" (left panel) ⇒ Right-click on your people package ⇒ New ⇒ Class The "New Java Class" dialog pops up  In "Name" field, enter "Student "  Click "Finish" Task - Write code for Student class Constructor public Student() { code = age = 0; name = ""; address = ""; project = ""; teacherCode = 0; teacherName = ""; } Instance variable to store student information private private private private private int code; String name; int age; String address; String project; Page 13 private int teacherCode; //store code of the teacher who student want to work with String teacherName; //store name of the teacher who student want to work with } Method to display the details of Student public void getStudentDetails() { System.out.println("The details about the student are:"); System.out.println("Code: " + code); System.out.println("Age: " + age); System.out.println("Address: " + address); System.out.println("Project: " + project); System.out.print("Teacher attending the student: " + teacherName); } Method to accept the details of Student public void setStudentDetails() { Scanner input = new Scanner(System.in); System.out.println("Enter System.out.println("Enter code = input.nextInt(); System.out.println("Enter name = input.next(); System.out.println("Enter age = input.nextInt(); System.out.println("Enter address = input.next(); details about the student:"); student code:" ); name:"); age:" ); address:"); System.out.println("Enter Project: You can type as many lines as " + "you wish Enter to stop."); // Accept the details about various project until the user types a zero project = new Scanner(System.in).useDelimiter("0").next(); } Method to assign a teacher to a student public void assignTeacher(String name, int code) { this.teacherName = name; this.teacherCode = code; } Page 14 Task - Write Program Class in a different package, use classes in other package Create a Admin package Create Program Class belong to Admin package Import class Student and Teacher import people.Student; import people.Teacher; Write code for main() method in Program Class public static void main(String[] args) { // Create a new teacher Teacher objTeacher = new Teacher(); // Accept details about the teacher objTeacher.setTeacherInfo(); // Create a new Student Student objStudent = new Student(); // Accept patient details objStudent.setStudentDetails(); // Assign a teacher to the patient objStudent.assignTeacher(objTeacher.getName(), objTeacher.getCode()); // Display the patient details objStudent.getStudentDetails(); } Page 15 Task - Execute your program To run the program, right-click anywhere on the source file "Program.java" (or from the "Run" menu) ⇒ Choose "Run As" ⇒ "Java Application" The output appears on the "Console" panel Next Step Summary In this lab, you have learned some basic of Orient Object Programming, you haved created classes and packages, declared some kind of variables and method Moreover, you have create instance objects from classes and use them to write applications Page 16 ... in other package Exercises This Hands-On Lab is comprised by the following exercises:  Booking Room  Project Assignment Estimated time to complete this lab: hours Page Index EXERCISE 1: BOOKING... Orient Object Programming and apply it into these exercises Objectives Once you have completed this lab, you will understand:  How to create a class  How to create fields and methods for a class...Overview In this lab, you will write simple applications to practice creating classes and packages in Java You have

Ngày đăng: 03/10/2016, 00:14

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w