Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Lesson 3 125 Self-Check 11 1. How are type wrappers used in Java? XXXXXXXXXX__________________________________________________________ 2. What is boxing?...

1 answer below »
Lesson 3 125
Self-Check 11
1. How are type wrappers used in Java?
XXXXXXXXXX__________________________________________________________
2. What is boxing?
XXXXXXXXXX__________________________________________________________
3. What is autoboxing?
XXXXXXXXXX__________________________________________________________
4. What primitive type is associated with the Integer wrapper class?
XXXXXXXXXX__________________________________________________________
5. Given the following method declaration, which type of objects are allowed for the a
ay
and elem parameters?
void sortA
ay (T[] a
ay, T elem)
XXXXXXXXXX__________________________________________________________
XXXXXXXXXX__________________________________________________________
Check your answers with those on page 205.
Programming in Java126
NOTES
Lesson 3 127
Object-Oriented
Programming
OVERVIEW
After reading selected sections of Chapters 4, 6, 7, 8, 12, and 13
in the textbook and completing Lesson 3, you’re ready to begin
coding with object-oriented programming (OOP) on your own.
This project will assess your understanding of creating a
class hierarchy in a package and writing code for classes and
enumerations.
Make sure that you follow all directions completely and verify
your results before submitting the project. Remember to include
all required components in your solution.
YOUR PROJECT
In this project, you’ll create data types in a class structure fo
cell-based board games similar to Tic-Tac-Toe. Games like
Connect Four and Mastermind also use boards divided by
ows and columns. The Board and Cell classes represent the
oard, while the Player, Mark, and Outcome enumerations
track the game.
You’ll use the classes and enumerations created in this project
in future graded projects. You use the NetBeans project in
the next lesson.
G
a
d
e
d
P
o
je
c
t
G
a
d
e
d
P
o
je
c
t
Programming in Java128
INSTRUCTIONS
1. In NetBeans, create a new Java Application project
named BoardGameTester.
2. Create a new package named games and a sub-package of
games named board. The easiest way is simply to create
a package named games.board.
3. Add an enumeration named Player to the games.board
package. You could add Empty Java File or choose
Java Enum as the file type. This enumeration represents
the cu
ent player in a turn-based game. Use the follow-
ing code:
public enum Player {FIRST,SECOND}
Note: Although Tic-Tac-Toe and Mastermind allow only
two players, Connect Four can be played with up to fou
players. For simplicity, our code will handle only two
players.
4. Add an enumeration named Outcome to the
games.board package. This enumeration represents the
esult when the turn is completed. Use the following
code:
5. Add an enumeration named Mark to the games.board
package. This enumeration represents the result when
the game is completed. Use the following code:
Keep in mind that only yellow and red are used in
Connect Four, while Mastermind uses all six colors.
public enum Outcome {PLAYER1_WIN, PLAYER2_WIN,
CONTINUE, TIE}
public enum Mark {EMPTY, NOUGHT, CROSS, YELLOW, RED,
BLUE, GREEN, MAGENTA, ORANGE}
Lesson 3 129
- 6. Add the Cell class to the games.board package. It should
have the private variables content, row, and column,
and the public methods getContent, setContent,
getRow, and getColumn. Use the following code as a
guide:
public class Cell {
private Mark content;
private int row, column;
public Cell(int row, int column) {
this.row = row;
this.column = column;
content = Mark.EMPTY;
}
public Mark getContent() { return content; }
Take note that all classes that support direct instantiation
should have a constructor. In this case, the constructo
will be used by the Board class to create each of its cells.
public void setContent(Mark content) { this.content = content;
}
public int getRow() { return row; }
public int getColumn() { return column; }
}
Programming in Java130
7. Add the Board class to the games.board package. It
should have a two-dimensional a
ay of Cell objects.
The Board class should initialize a board with a specified
number of columns and rows, provide access to Cell
objects, and display all of its cells co
ectly. Use the
following code as a guide:
public class Board {
private Cell[][] cells;
public Board(int rows, int columns) {
cells = new Cell[rows][columns];
for( int r = 0; r < cells[0].length; r++
) {
for (int c = 0; c < cells[1].length;
c++) {
cells[r][c] = new Cell(r,c);
}
}
}
public void setCell(Mark mark, int row, int
column) throws IllegalArgumentException {
if (cells[row][column].getContent() ==
Mark.EMPTY)
cells[row][column].setContent(mark);
else throw new
IllegalArgumentException(“Player already
there!” );
}
public Cell getCell(int row, int column) {
eturn cells[row][column];
}
public String toString() {
StringBuilder str = new StringBuilder();
for( int r = 0; r < cells.length; r++ ) {
str.append(“|” );
for (int c = 0; c < cells[r].length;
c++) {
switch(cells[r][c].getContent())
{
Lesson 3 131
case NOUGHT:
str.append(“O” );
eak;
case CROSS:
str.append(“X” );
eak;
case YELLOW:
str.append(“Y” );
eak;
case RED:
str.append(“R” );
eak;
case BLUE:
str.append(“B” );
eak;
case GREEN:
str.append(“G” );
eak;
case MAGENTA:
str.append(“M” );
eak;
case ORANGE:
str.append(“M” );
eak;
default:
Empty
str.append(“ “);
}
str.append(“|” );
}
str.append(“\n” );
}
eturn str.toString();
}
}
Programming in Java132
This code should seem familiar to you. The methods in
the TicTacToeGame class are similar to those in the
Board class. The StringBuilder class was used instead
of the String class for better performance. You can learn
more about the StringBuilder class by visiting the
Oracle Website at http:
docs.oracle.com/javase/
tutorial/java/data
uffers.html.
8. Add the following import statement to the
BoardGameTester class:
import games.boards.*;
9. In the main() method of BoardGameTester, perform
the following actions:
a. Create a 3 � 3 board for a Tic-Tac-Toe game.
. Create a 6 � 7 board for a Connect Four game.
c. Create a 5 � 8 board for a game of Mastermind.
d. Set a cell to a nought or cross on the Tic-Tac-Toe
oard.
e. Set a cell to yellow or red on the Connect Fou
oard.
f. Set a cell to yellow, red, green, blue, magenta, o
orange on the Mastermind board.
g. Display the boards for Tic-Tac-Toe, Connect Four,
and Mastermind.
10. Compile and run the project to ensure it works as you
expected it to.
Lesson 3 133
SUBMISSION GUIDELINES
To submit your project, you must provide the following
source code files in a ZIP file:
� BoardGameTester.java
� Board.java
� Cell.java
� Mark.java
� Outcome.java
� Player.java
To find these files in NetBeans, go to the BoardGameTeste
project folder. To determine this folder, right-click on the
BoardGameTester project in the Projects panel. Copy the
value for the Project Folder textbox using the keyboard
shortcut CTRL+C. In Windows Explorer, paste the project
folder path and hit the ENTER key. Right-click on the src
folder and choose the Send to... > Compressed (zipped)
folder option from the context menu. Rename the file to
BoardGameTester_Lesson3.zip and copy it to your desktop
or any other temporary location.
Follow this procedure to submit your project online:
1. Log on to the Penn Foster website and go to student por-
tal.
2. Click Take Exam.
3. Attach your Zip file as follows:
a. Click on the Browse box.
. Locate the file you wish to attach.
c. Double-click on the file.
d. Click Upload File.
Programming in Java134
4. Enter your e-mail address in the box provided. (Note:
This information is required for online submissions.)
5. If you wish to tell your instructor anything specific
egarding this assignment, enter it in the Message box.
6. Click Submit File.
Grading Criteria
Your instructor will use the following guidelines to grade
your project.
All types are organized in co
ect
package hierarchy 10 points
Mark enumeration co
ectly defined 5 points
Outcome enumeration co
ectly defined 5 points
Player enumeration co
ectly defined 5 points
Cell class co
ectly defined 10 points
Board class co
ectly defined 10 points
BoardGameTester class co
ectly defined 20 points
The application returns expected output 25 points
All source code files are included: 10 points
TOTAL 100 points
135
Advanced
Programming Logic
INTRODUCTION
So far the applications you’ve designed are transient and
unoptimized. They’re effective as long as a user keeps the
program running and performs one simple action at a time.
Well-built applications usually take advantage of a user’s
local storage and memory/CPU utilization. Increasingly,
applications are expected to implement complex tasks as
simply as possible. In this lesson, you’ll learn how to load
and save data to files, and to use multithreading techniques
and lambda expressions to improve the consistency and per-
formance of your applications.
OBJECTIVES
When you complete this lesson, you’ll be able to
� Differentiate between the I/O classes provided by Java
� Write data from an application to a file, then read data
from a file back into an application
� Access a local user’s system using the java.nio package
� Define lambda expressions and blocks
� Define functional interfaces
� Reference methods
� Use the java.util.function package
� Create a custom thread using the Thread class and
Runnable interface
� Control a running thread’s lifetime and priority
� Use thread synchronization and notification techniques
� Use the java.util.concu
ency package
L
e
s
s
o
n
4
L
e
s
s
o
n
4
Programming in Java136
ASSIGNMENT 12:
USE I/O CLASSES
Read Assignment 12 in this study guide. Then read Chapter 10
in your textbook.
Assignment 12 introduces basic input/output programming
in Java and addresses I/O classes in the java.nio package.
Be sure to complete the Try This activities in Chapter 10.
Standard Stream Classes
A stream is a sequential flow of data from a source to a
destination. Sources can include files, network hosts, or
even local String objects. Data sent through a stream can be
aw bytes, individual characters, sequences of characters, o
entire objects. Input streams read data from a stream, while
output streams write data to a stream.
There are many stream classes located in the java.io pack-
age, each specialized for
Answered Same Day Mar 10, 2021

Solution

Samrakshini R answered on Mar 11 2021
142 Votes
BoardGameTeste
.classpath
    
    
    
BoardGameTeste
.project
     BoardGameTeste
    
    
    
    
        
             org.eclipse.jdt.core.javabuilde
            
            
        
    
    
         org.eclipse.jdt.core.javanature
    
BoardGameTeste
.settings/org.eclipse.jdt.core.prefseclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=e
o
org.eclipse.jdt.core.compiler.problem.enumIdentifier=e
o
org.eclipse.jdt.core.compiler.source=1.8
BoardGameTeste
in/BoardGameTester.classpublic synchronized class BoardGameTester {
public void BoardGameTester();
public static void main(String[]);
}
BoardGameTeste
in/games
oard/Board.classpackage games.board;
public synchronized class Board {
private Cell[][] cells;
public void Board(int, int);
public void setCell(Mark, int, int) throws IllegalArgumentException;
public Cell getCell(int, int);
public String toString();
}
BoardGameTeste
in/games
oard/Cell.classpackage games.board;
public synchronized class Cell {
private Mark content;
private int row;
private int column;
public void Cell(int, int);
public Mark getContent();
public void setContent(Mark);
public int getRow();
public int getColumn();
}
BoardGameTeste
in/games
oard/Mark.classpackage games.board;
public final synchronized enum Mark {
public static final Mark EMPTY;
public static final Mark NOUGHT;
public static final Mark CROSS;
public static final Mark YELLOW;
public static final Mark RED;
public static final Mark BLUE;
public static final Mark GREEN;
public static final Mark MAGENTA;
public static final...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here