Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PURPOSE: Demonstrate your ability to ability to use Java classes including the u

ID: 3639624 • Letter: P

Question

PURPOSE:

Demonstrate your ability to ability to use Java classes including the use of correct scope for instance data; the structure of method definitions including parameters and return values; and, the correct structure and purpose of a constructor.

BACKGROUND THOUGHT:
Here are some hints that may make your classes more acceptable in well-mannered OOP circles:

1) Always keep data private
When data are kept private, changes in their representation do not affect the user of the class, and bugs are easier to detect

Explanation / Answer

import javax.swing.*; import java.awt.*; public class RandomSkyline { public static void main (String[] args) { //-------------------------------------------------------------------------------- // Creates the main frame of the program. //-------------------------------------------------------------------------------- JFrame frame = new JFrame ("Jett's City"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SkylinePanel()); frame.pack(); frame.setVisible(true); } } import javax.swing.*; 02 import java.awt.*; 03 import java.util.Random; 04 05 public class SkylinePanel extends JPanel 06 { 07 private Building building; 08 private Star star; 09 private Random generator; 10 private final int MAX_STARS = 35, MAX_BUILDINGS = 8; 11 12 //------------------------------------------------------------------------------ 13 // Constructor- Sets up drawing panel. 14 //------------------------------------------------------------------------------ 15 public SkylinePanel() 16 { 17 setBackground (Color.black); 18 setPreferredSize (new Dimension(800, 800)); 19 } 20 21 public void paintComponent(Graphics page) 22 { 23 Random generator = new Random(); 24 int numStars = generator.nextInt(MAX_STARS) + 35; 25 int xStar, yStar, WIDTHSTAR, HEIGHTSTAR; 26 super.paintComponent(page); 27 28 // Draws the ground. 29 page.setColor (Color.darkGray); 30 page.fillRect (0, 750, 800, 50); 31 32 // Draws text string. 33 page.setColor (Color.magenta); 34 page.drawString ("Welcome to Jett's City!!", 400, 775); 35 36 //------------------------------------------------------------------------------ 37 // Paints the stars in the sky. 38 //------------------------------------------------------------------------------ 39 40 for (int count = 0; count