Q1: please fill in the empty place (yellow), and run the code to get the output.
//********************************************************************
// FoodAnalyzer.java Author: Lewis/Loftus
// Demonstrates indirect access to inherited private members.
//********************************************************************
//********************************************************************
// Pizza.java Author: Lewis/Loftus
//
// Represents a pizza, which is a food item. Used to demonstrate
// indirect referencing through inheritance.
1
//********************************************************************
Page
public class Pizza extends FoodItem
{
//-----------------------------------------------------------------
// Sets up a pizza with the specified amount of fat (assumes
// eight servings).
//-----------------------------------------------------------------
public Pizza (int fatGrams)
{
super (fatGrams, 8);
}
}
//********************************************************************
// FoodItem.java Author: Lewis/Loftus
//
// Represents an item of food. Used as the parent of a derived class
// to demonstrate indirect referencing.
//********************************************************************
//-----------------------------------------------------------------
2
// Sets up this food item with the specified number of fat grams
Page
// and number of servings.
//-----------------------------------------------------------------
public FoodItem (int numFatGrams, int numServings)
{
fatGrams = numFatGrams;
servings = numServings;
//-----------------------------------------------------------------
// Computes and returns the number of calories in this food item
// due to fat.
//-----------------------------------------------------------------
private int calories()
{
return fatGrams * CALORIES_PER_GRAM;
}
//-----------------------------------------------------------------
// Computes and returns the number of fat calories per serving.
//-----------------------------------------------------------------
public int caloriesPerServing()
{
return (calories() / servings);
}
}
3
Page
Run them and the Output: _______ Calories per serving: 309
Q2: please fill in the empty place (yellow), and run the code to get the output.
//********************************************************************
// OffCenter.java Author: Lewis/Loftus
//
// Demonstrates the use of an event adatpter class.
//********************************************************************
import javax.swing.*;
frame.getContentPane().add(new OffCenterPanel());
frame.pack();
frame.setVisible(true);
}
}
//********************************************************************
// OffCenterPanel.java Author: Lewis/Loftus
//
// Represents the primary drawing panel for the OffCenter program.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
//-----------------------------------------------------------------
// Constructor: Sets up the panel and necessary data.
//-----------------------------------------------------------------
public OffCenterPanel()
{
addMouseListener (new OffCenterListener());
centerX = WIDTH / 2;
centerY = HEIGHT / 2;
//-----------------------------------------------------------------
// Draws a line from the mouse pointer to the center point of
// the applet and displays the distance.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
6
Page
page.setColor (Color.black);
page.drawOval (centerX-3, centerY-3, 6, 6);
if (current != null)
{
page.drawLine (current.x, current.y, centerX, centerY);
page.drawString ("Distance: " + fmt.format(length), 10, 15);
}
}
//*****************************************************************
// Represents the listener for mouse events. Demonstrates the
// ability to extend an adaptor class.
//*****************************************************************
private class OffCenterListener extends MouseAdapter
{
//--------------------------------------------------------------
// Computes the distance from the mouse pointer to the center
// point of the applet.
//--------------------------------------------------------------
public void mouseClicked (MouseEvent event)
{
current = event.getPoint();
length = Math.sqrt(Math.pow((current.x-centerX), 2) +
Math.pow((current.y-centerY), 2));
repaint();
}
7
}
Page
}
Run them and the Output: ____________________________
Note: Screenshot is down
8
Page
Q3: please fill in the empty place (red), and run the code to get the output.
//********************************************************************
// Rebound.java Author: Lewis/Loftus
//
// Demonstrates an animation and the use of the Timer class.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
frame.getContentPane().add(new ReboundPanel());
frame.pack();
frame.setVisible(true);
}
}
9
Page
//********************************************************************
// ReboundPanel.java Author: Lewis/Loftus
//
// Represents the primary panel for the Rebound program.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//-----------------------------------------------------------------
// Sets up the panel, including the timer for the animation.
//-----------------------------------------------------------------
public ReboundPanel()
{
timer = new Timer(DELAY, new ReboundListener());
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
image.paintIcon (this, page, x, y);
}
//*****************************************************************
// Represents the action listener for the timer.
//*****************************************************************
private class ReboundListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
11
//--------------------------------------------------------------
Page
public void actionPerformed (ActionEvent event)
{
x += moveX;
y += moveY;
repaint();
}
}
}
12
Page
Q4: Design and implement a project: (fill in the yellow fields with one row or multiple rows
codes), and run the code to get the output.
Design and implement a class called MonetaryCoin that is derived from the Coin class (see
below). Store a value in the monetary coin that represents its value and add a method that returns
its value. Creat a main driver class (MonetaryCoinDriver) to instantiate and compute the sum of
several MonetaryCoin objects. Demonstrate that a monetary coin inherits its parents ability to be
flipped.
//********************************************************************
13
}
Page
//********************************************************************
// Coin.java Author: Lewis/Loftus
//********************************************************************
//-----------------------------------------------------------------
// Sets up the coin by flipping it initially.
//-----------------------------------------------------------------
public Coin ()
{
flip();
}
//-----------------------------------------------------------------
// Flips the coin by randomly choosing a face value.
//-----------------------------------------------------------------
public void flip ()
{
face = (int) (Math.random() * 2);
}
15
Page
//-----------------------------------------------------------------
// Returns true if the current face of the coin is heads.
//-----------------------------------------------------------------
public boolean isHeads ()
{
return (face == HEADS);
}
//-----------------------------------------------------------------
// Returns the current face of the coin as a string.
//-----------------------------------------------------------------
public String toString()
{
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
return faceName;
}
}
//********************************************************************
// MonetaryCoin.java
//********************************************************************
16
import java.text.*;
Page
public class MonetaryCoin extends Coin
{
private int coinValue;
NumberFormat money = NumberFormat.getCurrencyInstance();
public MonetaryCoin(int value)
{
coinValue = value;
}
Note:
Down is the screenshot
17
Page
Page
18