JAVA Programming Language Practice02: O-O Programming. 张 成 伟 Zhang Chengwei Phone: 1398 6214 512



Similar documents
IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

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

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:

APPLICATIONS AND MODELING WITH QUADRATIC EQUATIONS

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

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

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

3. How many winning lines are there in 5x5 Tic-Tac-Toe? 4. How many winning lines are there in n x n Tic-Tac-Toe?

Efficient Data Structures for Decision Diagrams

Chapter 13 - Inheritance

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

CISC 181 Project 3 Designing Classes for Bank Accounts

Java Application Developer Certificate Program Competencies

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

The Snake Game Java Case Study

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

Microsoft Access 3: Understanding and Creating Queries

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Chapter 7: Sequential Data Structures

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Unit Testing & JUnit

Description of Class Mutation Mutation Operators for Java

Object-Oriented Programming Lecture 2: Classes and Objects

AP Computer Science Java Subset

BCS2B02: OOP Concepts and Data Structures Using C++

3. Add an Event: Alarm Alarm 0 a. Add an Action: Set Variable i. Applies to: Self ii. Variable: time_left iii. Value: +1 iv. Check the Relative box

History OOP languages Year Language 1967 Simula Smalltalk

CmpSci 187: Programming with Data Structures Spring 2015

Software Engineering Techniques

Glossary of Object Oriented Terms

Chapter 3: Restricted Structures Page 1


Area and Perimeter: The Mysterious Connection TEACHER EDITION

El Dorado Union High School District Educational Services

Multiplication. Year 1 multiply with concrete objects, arrays and pictorial representations

The Interface Concept

(Eng. Hayam Reda Seireg) Sheet Java

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013


Three daily lessons. Year 5

Why? A central concept in Computer Science. Algorithms are ubiquitous.

An Introduction to Object-Oriented Programming with

Computer Programming I

Java (12 Weeks) Introduction to Java Programming Language

called and explain why it cannot be factored with algebra tiles? and explain why it cannot be factored with algebra tiles?

A Concrete Introduction. to the Abstract Concepts. of Integers and Algebra using Algebra Tiles

o Graph an expression as a function of the chosen independent variable to determine the existence of a minimum or maximum

Java Interview Questions and Answers

Envisioneer Technical Note

Inheritance, overloading and overriding

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Grade 5 Work Sta on Perimeter, Area, Volume

UNIVERSITY OF CALIFORNIA BERKELEY Engineering 7 Department of Civil and Environmental Engineering. Object Oriented Programming and Classes in MATLAB 1

Datum > Curve KIM,ME,NIU

Introduction to scripting with Unity

Free Pre-Algebra Lesson 55! page 1

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

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

COMPUTER SCIENCE. Paper 1 (THEORY)

WebSphere Business Monitor

Contents. 9-1 Copyright (c) N. Afshartous

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

LAB4 Making Classes and Objects

Part 1 Foundations of object orientation

To Evaluate an Algebraic Expression

3 Improving the Crab more sophisticated programming

Specialized Programme on Web Application Development using Open Source Tools

Visual Basic Programming. An Introduction

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

The C Programming Language course syllabus associate level

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

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

FACTORING QUADRATICS and 8.1.2

Assignment # 2: Design Patterns and GUIs

Section IV.1: Recursive Algorithms and Recursion Trees

CS 378 Big Data Programming. Lecture 24 RDDs

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Annotated work sample portfolios are provided to support implementation of the Foundation Year 10 Australian Curriculum.

Number Sense and Operations

Chapter 8: Bags and Sets

Field Properties Quick Reference

Computational Mathematics with Python

Logic in Computer Science: Logic Gates

Moving from CS 61A Scheme to CS 61B Java

WORK SCHEDULE: MATHEMATICS 2007

Sample Problems. Practice Problems

Komumaldata Barcoded PDF Forms PDF417 Barcode Generator Reference XFA (LiveCycle) Version Generator Version: 1.12

A generic framework for game development

Chapter 6: Programming Languages

Data Structures using OOP C++ Lecture 1

Introduction to Java. CS 3: Computer Programming in Java

Intel Retail Client Manager Audience Analytics

GED Practice Test #3 (34 Questions - each question is worth 2.94 points)

Sect Solving Equations Using the Zero Product Rule

Transcription:

JAVA Programming Language Practice02: O-O Programming 张 成 伟 Zhang Chengwei Email: Zhangcw@hust.edu.cn Phone: 1398 6214 512

PRACTICE 2 Java Programming Language 2

Exercise 8.4 (Rectangle Class) On book: Java How to Program(6th) [ link ] Create a class Rectangle. The class has attributes length and width, each of which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle. Java Programming Language 3

Exercise 8.6 (Savings Account Class) Create class SavingsAccount. Use a static variable annualinterestrate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsbalance indicating the amount the saver currently has on deposit. Provide method calculatemonthlyinterest to calculate the monthly interest by multiplying the savingsbalance by annualinterestrate divided by 12 this interest should be added to savingsbalance. Provide a static method modifyinterestrate that sets the annualinterestrate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsaccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualinterestrate to 4%, then calculate the monthly interest and print the new balances for both savers. Then set the annualinterestrate to 5%, calculate the next month's interest and print the new balances for both savers. Java Programming Language 4

Exercise 8.6: Sample output Java Programming Language 5

Exercise 8.12 Java Programming Language 6

Exercise 8.14 (Enhanced Rectangle Class) Create a more sophisticated Rectangle class than the one you created in Exercise 8.4. This class stores only the Cartesian coordinates of the four corners of the rectangle. The constructor calls a set method that accepts four sets of coordinates and verifies that each of these is in the first quadrant with no single x- or y- coordinate larger than 20.0. The set method also verifies that the supplied coordinates specify a rectangle. Provide methods to calculate the length, width, perimeter and area. The length is the larger of the two dimensions. Include a predicate method issquare which determines whether the rectangle is a square. Write a program to test class Rectangle. Java Programming Language 7

Exercise 8.15 (Set of Integers) Create class IntegerSet. Each IntegerSet object can hold integers in the range 0100. The set is represented by an array of booleans. Array element a[i] is true if integer i is in the set. Array element a[j] is false if integer j is not in the set. The no-argument constructor initializes the Java array to the "empty set" (i.e., a set whose array representation contains all false values). Provide the following methods: Method union creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third set's array is set to true if that element is true in either or both of the existing sets otherwise, the element of the third set is set to false). Method intersection creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set's array is set to false if that element is false in either or both of the existing setsotherwise, the element of the third set is set to true). Method insertelement inserts a new integer k into a set (by setting a[k] to true). Method deleteelement deletes integer m (by setting a[m] to false). Method tosetstring returns a string containing a set as a list of numbers separated by spaces. Include only those elements that are present in the set. Use --- to represent an empty set. Method isequalto determines whether two sets are equal. Write a program to test class IntegerSet. Instantiate several IntegerSet objects. Test that all your methods work properly. Java Programming Language 8

Exercise 8.19 (Tic-Tac-Toe) Create an project TicTacToe that will enable you to play the game of Tic-Tac-Toe. The game contains a private 3-by-3 two-dimensional array of integers. The first step should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it is a draw. If you feel ambitious, modify your program so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second. Java Programming Language 9

Exercise 8.19: Sample output Java Programming Language 10

Learning Issues Class/Object Class Members: attributes and behaviors Attributes: variables are defined in the class scope Behaviors: methods are defined in the class scope Define a class Class constructors Method overloading/ Constructor overload Create an object from the existing class public/private/package access Java package (import/package) Keywords: this, static, final Java Programming Language 11