© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_32

32. Projects

Ron Dai1 
(1)
Seattle, WA, USA
 

I want to recommend a list of hands-on projects for you to practice independently. Working through these projects will definitely help you deepen your understanding of the basic Java programming concepts described in this book.

Project A

Step 1

Write a class called Rectangle that represents a rectangular two-dimensional region. The constructor creates a new rectangle whose top-left corner is specified by the given coordinates and with the given width and height.
public Rectangle(int x, int y, int width, int height)
Your Rectangle objects should have the following methods:
  • public int getHeight() - Returns this rectangle’s height.

  • public int getWidth() - Returns this rectangle’s width.

  • public int getX() - Returns this rectangle’s x-coordinate.

  • public int getY() - Returns this rectangle’s y-coordinate.

  • public String toString() - Returns a string representation of this rectangle, such as:

"Rectangle[x=1,y=2,width=3,height=4]"

Step 2

Add the following accessor methods to your Rectangle class from the previous exercises:
public boolean contains(int x, int y)
public boolean contains(Point p)
The Point class has been defined as shown:
public class Point {
       private int x;
       private int y;
       public Point(int x, int y) {
              this.x = x;
              this.y = y;
       }
       public int getX() {
              return x;
       }
       public int getY() {
              return y;
       }
}

The two contains() methods return a Boolean state of whether the given Point or coordinates lie inside the bounds of this Rectangle or not. For example, a rectangle with [x=2, y=5, width=8, height=10] will return true for any point from (2, 5) through (10, 15) inclusive, which means the edges are included.

Project B

Design a program to find the number of days between the current day and the user’s birthday, given four input values.

The program prompts for the user’s birthday. The prompt lists the range of values from which to choose. Notice that the range of days printed is based upon the number of days in the month the user typed. The program prints the absolute day of the year for the birthday. January 1st is absolute day #1 and December 31st is absolute day #365. Last, the program prints the number of days until the user’s next birthday. Different messages appear if the birthday is today or tomorrow. The following are four runs of your program and their expected output (user input data is right after the ‘?’ mark):
Please enter your birthday:
What is the month (1-12)? 11
What is the day (1-30)? 6
11/6 is day #310 of 365.

Your next birthday is in 105 days, counted from today.

Project C

The game rule is this: you start with 21 sticks, and two players take turns either taking one or two sticks. The player who takes the last stick loses. Can you design a program to simulate one of the two players in the game? One player is a user and the other player is the computer.

Project D

Write a method named hasVowel() that returns whether a string has included any vowel (a single-letter string containing a, e, i, o, or u, case-insensitively).

Project E

Write a method named gcd() that accepts two integers as parameters and returns the greatest common divisor (GCD) of the two numbers. The GCD of two integers a and b is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and 0 is the number.

One efficient way to compute the GCD of two numbers is to use Euclid’s algorithm, which states the following:

GCD(A, B) = GCD(B, A % B)

GCD(A, 0) = Absolute value of A

For example:
  • gcd(24, 84) returns 12

  • gcd(105, 45) returns 15

  • gcd(0, 8) returns 8

Project F

Write a method named toBinary() that accepts an integer as a parameter and returns a string of that number’s representation in binary. For example, the call of toBinary(42) should return “101010”.

Project G

Use the four numbers on the following cards to create a math expression that equals 24. Each card can be used only once. Treat ace as a number “1”. You may use +, -, *, /, ( and ) in the math expression. Please find all possible answers.

../images/485723_1_En_32_Chapter/485723_1_En_32_Figa_HTML.jpg
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.89.24