Question

In: Computer Science

Complete Chapter 5, Programming Activity 1: Working with "if/else". Make sure you study the Programming Activity...

Complete Chapter 5, Programming Activity 1: Working with "if/else". Make sure you study the Programming Activity 5-1 Guidance document. Which part of the guidance document was the most helpful to you? Why? At the end of your student code, also add the following statement: System.out.println("Score = " + score + " and result = " + result); This code displays your code's result for each input value in the console window. Discuss your experience writing and testing this activity. Discuss the usefulness of the program's visual output in helping you understand the flow of your code. Discuss why you think this programming activity was easy or difficult.

Programming Activity 5-1 Guidance ================================= Input parameter --------------- The parameter "score" is passed into the function where you put your code. Therefore, it is available for you to use in your code. Outputs ------- There are 3 possible outputs: "over par" "par" "below par" It is correct for the JOptionPane to pop-up after each time you enter a value. This allows you to try each test value without having to rerun the application. When you are done testing, click its Cancel button or the red X in the upper right corner of its window. Source files ------------ For all of the textbook activities, you must copy all of the source files provided in our course Textbook Activity Frameworks folder. When you compile or run the program, you must be in the correct source file in TextPad. For this activity, it is the PathClient.java file. Test cases ---------- Make sure you test with each of the recommended test cases: 45, 71, 72, and 73. Not every assignment will tell you the recommended test cases. When they are not provided, you must determine representative test cases.

Solutions

Expert Solution

// Path.java

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;

public class Path
{

private int inputScore; // the score
private String codeResult; // Over Par or Par or less
private String correctness; // correct or incorrect
private String verb; // is (correct) or was (incorrect)

private int startX = 50;
private int startY = 25;
private int startXFace = 425;
private int startYFace = 125;

private double c = 0.5; // coefficient for the size of the happy/unhappy faces

private final Color GREENISH = new Color( 127, 255, 212 );
private Color leftSideColor;
private Color rightSideColor;

private Color overParColor;
private Color parColor;
private Color belowParColor;

public Path( )
{
inputScore = -1;
codeResult = "unknown";
correctness = "unknown";
verb = "unknown";
}

public void setInputScore( int is )
{
inputScore = is;
}

public void setCodeResult( String cr )
{
codeResult = cr;
}

public void setCorrectness( )
{
if (inputScore > 72 && codeResult.equalsIgnoreCase( "over par" ) )
{
correctness = "correct";
verb = "is";
}
else if ( inputScore < 72 && codeResult.equalsIgnoreCase( "below par" ) )
{
correctness = "correct";
verb = "is";
}
else if ( inputScore == 72 && codeResult.equalsIgnoreCase( "par" ) )
{
correctness = "correct";
verb = "is";
}
else
{
correctness = "incorrect";
verb = "was";
}
}

public void setSideColors( )
{
if ( inputScore > 72 && correctness.equals( "correct" ) )
{
leftSideColor = GREENISH;
rightSideColor = Color.LIGHT_GRAY;
}
else if ( inputScore <= 72 && correctness.equals( "correct" ) )
{
rightSideColor = GREENISH;
leftSideColor = Color.LIGHT_GRAY;
}
else if ( inputScore > 72 && correctness.equals( "incorrect" ) )
{
leftSideColor = GREENISH;
rightSideColor = Color.LIGHT_GRAY;
}
else
{
rightSideColor = GREENISH;
leftSideColor = Color.LIGHT_GRAY;
}
}

public void setSideCorrectColors( )
{
parColor = Color.LIGHT_GRAY;
overParColor = Color.LIGHT_GRAY;
belowParColor = Color.LIGHT_GRAY;

if ( inputScore > 72 )
{
overParColor = GREENISH;
}
else if ( inputScore == 72 )
{
parColor = GREENISH;
}
else
{
belowParColor = GREENISH;
}
}

public void draw( Graphics g )
{
// set correctness value
setCorrectness( );
// draw Face
drawFace( g );
// draw right path (correct path according to input)
drawCorrectPath( g );
// draw code inside rectangles
drawCode2( g );
// draw a String representing the score entered
drawScore( g );
// draw a String representing the result given by the code
drawCodeResult( g );
// draw a String representing whether the result is correct or incorrect
drawResultCorrectness( g );
// draw Headers
drawHeaders( g );
}

public void drawCorrectPath( Graphics g )
{
g.setColor( GREENISH );

// entry line to "> 72 ?"
g.fillRect( startX + 100, startY + 50, 3, 25 );

// "> 72 ?"
g.fillRoundRect( startX + 50, startY + 75, 100, 40, 10, 10 );

// "continue"
g.fillRoundRect( startX + 50, startY + 355, 100, 40, 10, 10 );

// "continue" to out
g.fillRect( startX + 100, startY + 395, 3, 25 );

///////// over par path drawing //////////////////////////////////
g.setColor( overParColor );

// line to "over par" assignment
g.drawLine( startX + 100, startY + 115, startX + 20, startY + 165 );
g.drawLine( startX + 99, startY + 115, startX + 19, startY + 165 );
g.drawLine( startX + 101, startY + 115, startX + 21, startY + 165 );

// "over par" assignment
g.fillRoundRect( startX - 30, startY + 165, 100, 40, 10, 10 );

// straight line from "over par" assignment
g.drawLine( startX + 20, startY + 205, startX + 20, startY + 305 );
g.drawLine( startX + 19, startY + 205, startX + 19, startY + 305 );
g.drawLine( startX + 21, startY + 205, startX + 21, startY + 305 );

// left line to continue
g.drawLine( startX + 20, startY + 305, startX + 100, startY + 355 );
g.drawLine( startX + 19, startY + 305, startX + 99, startY + 355 );
g.drawLine( startX + 21, startY + 305, startX + 101, startY + 355 );

///////// par and below par path drawing /////////////////////////
if ( inputScore <= 72 )
g.setColor( GREENISH );
else
g.setColor( Color.LIGHT_GRAY );
// line to test for == 72
g.drawLine( startX + 100, startY + 115, startX + 180, startY + 165 );
g.drawLine( startX + 99, startY + 115, startX + 179, startY + 165 );
g.drawLine( startX + 101, startY + 115, startX + 181, startY + 165 );

// test for == 72
g.fillRoundRect( startX + 130, startY + 165, 100, 40, 10, 10 );

///////// par path drawing ///////////////////////////////////////
g.setColor( parColor );

// line to "par" assignment
g.drawLine( startX + 180, startY + 205, startX + 100, startY + 255 );
g.drawLine( startX + 179, startY + 205, startX + 99, startY + 255 );
g.drawLine( startX + 181, startY + 205, startX + 101, startY + 255 );

// "par" assignment
g.fillRoundRect( startX + 50, startY + 255, 100, 40, 10, 10 );

// straight line from "par" assignment to continue
g.drawLine( startX + 100, startY + 295, startX + 100, startY + 355 );
g.drawLine( startX + 99, startY + 295, startX + 99, startY + 355 );
g.drawLine( startX + 101, startY + 295, startX + 101, startY + 355 );

///////// below par path drawing /////////////////////////////////
g.setColor( belowParColor );

// line to "below par" assignment
g.drawLine( startX + 180, startY + 205, startX + 260, startY + 255 );
g.drawLine( startX + 179, startY + 205, startX + 259, startY + 255 );
g.drawLine( startX + 181, startY + 205, startX + 261, startY + 255 );

// "below par" assignment
g.fillRoundRect( startX + 210, startY + 255, 100, 40, 10, 10 );

// line from "below par" assignment to continue
g.drawLine( startX + 260, startY + 295, startX + 100, startY + 355 );
g.drawLine( startX + 259, startY + 295, startX + 99, startY + 355 );
g.drawLine( startX + 261, startY + 295, startX + 101, startY + 355 );
}

public void drawFace( Graphics g )
{
if ( correctness.equals( "correct" ) )
drawHappyFace( g );
else
drawUnHappyFace( g );
}

public void drawHappyFace( Graphics g )
{
// draw happy face
// ep xface sub 125,expression yface add 175

g.setColor( Color.YELLOW );
g.fillOval( startXFace, startYFace, (int) ( c * 150 ), (int) ( c * 150 ) );

// draw eyes
g.setColor( Color.BLACK );
g.drawLine( (int) ( startXFace + 35 * c ), (int) ( startYFace + 50 * c ),
(int) ( startXFace + 35 * c + c * 25 ), (int) ( startYFace + 50 * c ) );
g.drawLine( (int) ( startXFace + 85 * c ), (int) ( startYFace + 50 * c ),
(int) (startXFace + 85 * c + c * 25 ), (int) ( startYFace + 50 * c ) );

// draw nose
g.drawLine( (int) ( startXFace + 72 * c ), (int) ( startYFace + 70 * c ),
(int) ( startXFace + 72 * c ), (int) ( startYFace + 70 * c + 20 * c ) );

// draw mouth
g.drawArc( (int) ( startXFace + 35 * c ), (int) ( startYFace + 50 * c ),
(int) ( 75 * c ), (int) ( 75 * c ), 0, -180 );
}

public void drawUnHappyFace( Graphics g )
{
// draw unhappy face

g.setColor( Color.YELLOW );
g.fillOval( startXFace, startYFace, (int) (150*c), (int) (150*c));

// draw eyes
g.setColor( Color.BLACK );
g.drawLine( (int) (startXFace + 35*c), (int) (startYFace + 25*c), (int) (startXFace + 60*c), (int) (startYFace + 25*c) );
g.drawLine( (int) (startXFace + 85*c), (int) (startYFace + 25*c), (int) (startXFace + 110*c), (int) (startYFace + 25*c) );

// draw nose
g.drawLine( (int) (startXFace + 72*c), (int) (startYFace + 45*c), (int) (startXFace + 72*c), (int) (startYFace + 65*c) );

// draw mouth
g.drawArc( (int) (startXFace + 35*c), (int) (startYFace + 75*c), (int) (75*c), (int) (50*c), 0, 180 );

// draw tear

// center to right - down
g.drawArc( (int) (startXFace + 48*c), (int) (startYFace + 23*c), (int) (10*c), (int) (20*c), -90, -105 );

// center to left - down
g.drawArc( (int) (startXFace + 33*c), (int) (startYFace + 20*c), (int) (15*c), (int) (25*c), 0, -45 );

// circle
g.drawArc( (int) (startXFace + 43*c), (int) (startYFace + 40*c), (int) (10*c), (int) (10*c), 30, -270 );
}

public void drawScore( Graphics g )
{
g.setFont( new Font( "Serif", Font.BOLD, 18 ) );
g.setColor( Color.BLACK );
g.drawString( "The score was " + inputScore, startX, startY + 450 );
}

public void drawCodeResult( Graphics g )
{
g.setFont( new Font( "Serif", Font.BOLD, 18 ) );
g.setColor( Color.BLACK );
g.drawString( "The result given by your code is ", startX, startY + 500 );
g.setColor( Color.BLUE );
g.drawString( codeResult, startX + 250, startY + 500 );
g.setColor( Color.BLACK );
}

public void drawResultCorrectness( Graphics g )
{
g.setFont( new Font( "Serif", Font.BOLD, 18 ) );
g.setColor( Color.BLACK );
g.drawString( "The result given by your code is ", startX, startY + 550 );
if (correctness.equals( "correct" ) )
g.setColor( Color.BLUE );
else
g.setColor( Color.RED );
g.drawString( correctness.toUpperCase(), startX + 250, startY + 550 );
g.setColor( Color.BLACK );
}

public void drawHeaders( Graphics g )
{
g.setColor( Color.RED );
g.setFont( new Font( "Serif", Font.BOLD, 18 ) );
g.drawString( "The correct path " + verb, startX + 20, startY + 25 );
}

public void drawCode2( Graphics g )
{
g.setColor( Color.BLACK );
g.drawString( " score > 72 ?", startX + 60, startY + 90 );
g.drawString( "Yes: left No: right", startX + 56, startY + 110 );

g.drawString( "true:", startX + 10, startY + 180 );
g.drawString( "result = over par", startX - 20, startY + 200 );

g.drawString( "false: score = 72 ?", startX + 130, startY + 180 );
g.drawString( "Yes: left No: right", startX + 130, startY + 200 );

g.drawString( "true:", startX + 85, startY + 270 );
g.drawString( "result = par", startX + 70, startY + 290 );

g.drawString( "false:", startX + 245, startY + 270 );
g.drawString( "result = below par", startX + 210, startY + 290 );

g.drawString( "Continue", startX + 70, startY + 380 );
}
}

=======================================================================

// PathClient

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PathClient extends JFrame
{
private boolean firstTime = true;

private int key; // input from the user
private Path pathObject;

public PathClient( )
{
super( "Illustrating if .. else" );

pathObject = new Path( );

setSize( 600, 600 );
setVisible( true );
}

public void workWithIfElse( int score )
{
String result = "???";
// ***** Student code starts here
// If score is greater than 72, assign "over par" to result
// If score is equal to 72, assign "par" to result
// If score is less than 72, assign "below par" to result
//
if (score > 72)
result = "over par";
else if (score == 72)
result = "par";
else if (score < 72)
result = "below par";
//
// Student code ends here
//

firstTime = false;
animate( score, result );
}
// end of workWithIfElse

public void startActivity( )
{
boolean goodInput = false;
while ( !goodInput )
{
try
{
String answer = JOptionPane.showInputDialog( null,
"Enter your golf score" );

if ( answer != null )
{
key = Integer.parseInt( answer );
goodInput = true;
}
else
{
System.exit( 0 );
}
}
catch( Exception e )
{}
}
if ( goodInput )
{
workWithIfElse( key );
}
}

private void animate( int score, String result )
{
pathObject.setInputScore( score );
pathObject.setCodeResult( result );
pathObject.setSideCorrectColors( );
pathObject.setSideColors( );
try
{
repaint( );
Thread.sleep( 100 );
}
catch ( InterruptedException e )
{
System.out.println( "IE Exception " + e.getMessage( ) );
System.out.println( e.toString( ) );
}
}

public void paint( Graphics g )
{
super.paint( g );
if ( !firstTime )
pathObject.draw( g );
}

public static void main( String [] args )
{
PathClient app = new PathClient( );
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
PrintArrayT t = new PrintArrayT( app );
t.start( );
}
}

======================================================================

//PrintArrayT.java

public class PrintArrayT extends Thread
{
PathClient p1;

public PrintArrayT( PathClient pc )
{
p1 = pc;
}

public void run( )
{
while( true )
{
try
{
Thread.sleep( 500 );
}
catch( Exception e )
{}
p1.startActivity( );
}
}
}

========================================================================

sample output:


Related Solutions

Make sure to write in complete sentences and realte it to children nothing else Explain how...
Make sure to write in complete sentences and realte it to children nothing else Explain how children reason in Kohlberg’s three levels of moral thought: preconventional moral thought, conventional moral thought, and postconventional moral thought. For each level, report a) what children emphasize as important as their basis for their moral beliefs and b) provide an example from the Heinz dilemma that reflects that type of moral reasoning. Each explanation (what children emphasize)
3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design...
3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design in C book. All input should be read from a file and output should be written to a file. Make sure that you design a function greatest_common_divisor() which calculates and returns the greatest common divisor of two integer numbers. Also, develop functions to read data from a file and to write data to a file. Problem Statement: The greatest common divisor (gcd) of two...
Make sure to read the activity notes before you attempt this activity. Detailed instructions for completing...
Make sure to read the activity notes before you attempt this activity. Detailed instructions for completing the activity are included in the notes.   Make sure to work all the problems in this activity. The activity includes three problems, each on a different worksheet (see the tabs below: Problem 1,   Problem 2, etc.) Red Book Inc. Book sales during the first four months of the year were: Month # of Books Sold Jan               39 Feb               88 Mar               46...
Criteria for Success: To be successful you will make sure you complete diagrams in a concise...
Criteria for Success: To be successful you will make sure you complete diagrams in a concise manner that clearly explains or represents the anatomy and physiological responses. You also need to make sure to list or provide explanation where necessary or where it is asked in the tasks. A successful submission would be one that covers all questions for each scenarios and while you may need to do some extra research outside of canvas, please do not bring research level...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. This is a good reference for how throwing exceptions works in java. I would encourage you...
USE THE 5 STEP METHOD TO ANALYZE THE RESEARCH PROBLEM. MAKE SURE TO SHOW COMPLETE WORK....
USE THE 5 STEP METHOD TO ANALYZE THE RESEARCH PROBLEM. MAKE SURE TO SHOW COMPLETE WORK. A researcher tests the question of whether participation in community-sponsored services (such as card games, field trips, etc.) increases the quality of life (as rated from 1 to 10) for older Americans. The research implements the treatment over a 6-month period and then, at the end of the treatment period, measures quality of life in the two groups (each consisting of 50 participants over...
Make sure you name files with test_yourname in question 1, 4, and 5 Q1: Add a...
Make sure you name files with test_yourname in question 1, 4, and 5 Q1: Add a user named as test_yourname in linux terminal. Take a screenshot if it has been added to the system file /etc/passwd Q2: What is grep? And why is it used for? (2 lines only) Q3: Use grep over a file of your choice, take a screenshot and explain the results. Q4: Can you store the output of top command in a file named as: test_yourname.txt...
Complete all of your lesson materials and assigned readings. Make sure that you are focusing on:...
Complete all of your lesson materials and assigned readings. Make sure that you are focusing on: Communication techniques that can be used to promote safety within the healthcare facility. How communication can assist in providing optimal patient care. You should be using complete sentences to answer the questions. Ensure that you are using correct grammar. In addition, support your answers using your textbook, course materials, credible internet resources, and scholarly journals. SkyScape is a great suggestion for assistance in completion...
Assignment: Operations Make sure you have completed the Learning Activity in this unit before you start...
Assignment: Operations Make sure you have completed the Learning Activity in this unit before you start this Assignment. Using the graphic below, please discuss the inputs, processes, and outputs that pertain to your operations of the business you chose, whether it is a service or a product oriented business. Using these three steps as a reference, how will you satisfy your customers? What core competencies will you have that will set you apart from your competitors? Pick at least two...
Please make sure that these infix and postfix equations have these answers nothing else: Infix: (3...
Please make sure that these infix and postfix equations have these answers nothing else: Infix: (3 * 4 - (2 + 5)) * 4 / 2 = valid expression 10 + 6 * 11 -(3 * 2 + 14) / 2 = valid expression Postfix: 9 3 / 6 / 4 * 10 - = -8 9 3 / 6 / 4 * -10 - = 12 (a) Using java.util.stack to write a java program to validate and calculate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT