CodeHS Unit 4 Rectangle Class

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the formula used to calculate the distance in miles between two geo locations?

  • return Math.sin(latitude)
  • return distanceFrom(other) * KM_PER_MILE (correct)
  • return Math.cos(longitude)
  • return arcLength * RADIUS (correct)

What are the fields in the TextMessage class?

message, sender, receiver

What is the default maximum number of players in the Player class?

10

What does the getArea method in the Circle class return?

<p>Area of the circle</p> Signup and view all the answers

The PI value in the MathOperations class is static.

<p>False (B)</p> Signup and view all the answers

Which methods in the Product class are overloaded?

<p>All of the above (D)</p> Signup and view all the answers

The method to get the winner of Rock, Paper, Scissors is called ___

<p>getWinner</p> Signup and view all the answers

Match the following classes with their primary function:

<p>Messages = Handles text message logic Fraction = Represents fractions with numerators and denominators Circle = Calculates area based on the radius Player = Tracks the number of players in a game</p> Signup and view all the answers

What is the purpose of the Rectangle class?

<p>To create rectangle objects that have width and height.</p> Signup and view all the answers

What method is used to get the area of a Rectangle object?

<p>getArea</p> Signup and view all the answers

What is the output of the RectangleTester program when a rectangle with width 5 and height 12 is created?

<p>Rectangle with width: 5 and height: 12</p> Signup and view all the answers

The CoinFlips class simulates flipping a coin a specified number of times.

<p>True (A)</p> Signup and view all the answers

What do the variables named 'Bart' and 'Lisa' represent in the Messages class?

<p>TextMessage objects</p> Signup and view all the answers

Which of the following classes encapsulates properties of a flower?

<p>Flower (C)</p> Signup and view all the answers

What method in the Dog class returns a description of the dog?

<p>toString</p> Signup and view all the answers

How is the distance calculated between two GeoLocation objects?

<p>Using the distanceFrom method (C)</p> Signup and view all the answers

What does the Student class represent?

<p>It represents a student with first name, last name, grade level, and GPA.</p> Signup and view all the answers

The _____ method returns the name and grade level of the student.

<p>toString</p> Signup and view all the answers

The BaseballPlayer class has a method to calculate the batting average.

<p>True (A)</p> Signup and view all the answers

What is the output of the Fraction class's toString method when the fraction is 1/2?

<p>1/2</p> Signup and view all the answers

Flashcards

Point Class Features

Stores x and y coordinates, includes a method to move the point and overrides toString() to display position.

Attributes of Student Class

Contains firstName, lastName, and gradeLevel, and includes a toString() method for representation.

TextMessage Class Design

Defines sender, receiver, and message, with a toString() method for formatted output.

CoinFlips Class

Simulates FLIPS number of coin tosses, counting heads and tails outcomes, and calculates percentages.

Signup and view all the flashcards

Longest Streak Tracker

Tracks longest consecutive "Heads" results from flips, resetting streak upon a "Tails" outcome.

Signup and view all the flashcards

GeoLocation Class

Contains latitude and longitude data; includes method for distance calculation based on spherical law of cosines.

Signup and view all the flashcards

GPA Attribute Addition

Introduces a GPA field to student class with getter/setter methods and honors student determination.

Signup and view all the flashcards

Fraction Class

Represents rational numbers with numerator and denominator; includes toString() and getter/setter methods.

Signup and view all the flashcards

BaseballPlayer Class

Stores player stats, includes a method to calculate batting average, and formatted output for player performance.

Signup and view all the flashcards

Player Class

Maintains a static count of players, with a maximum limit and features construction to increase player count upon instantiation.

Signup and view all the flashcards

Unit Circle Calculation

Computes cosine and sine for angles in radians, displaying results for each calculated angle.

Signup and view all the flashcards

TextMessage Getter Methods

Includes getter methods for sender, receiver, and message content, facilitating information retrieval.

Signup and view all the flashcards

Fraction Getter/Setter Implementation

Adds methods to retrieve and modify numerator and denominator values, improving data encapsulation.

Signup and view all the flashcards

gameFull() Function

gameFull() checks if the total number of players has reached the maximum limit. Returns true if totalPlayers is greater than or equal to maxPlayers.

Signup and view all the flashcards

Circle Area Calculation

Class Circle defines a static constant PI for mathematical calculations. getArea() method calculates the area using the formula: Area = π * radius².

Signup and view all the flashcards

Rock, Paper, Scissors Winner Logic

If both choices are the same, it returns "Tie". Implements rules for winning: rock beats scissors, scissors beats paper, paper beats rock.

Signup and view all the flashcards

Randomizer Class

Singleton design pattern ensures only one instance of the Random object exists. Provides various random number generation methods.

Signup and view all the flashcards

Product Method Overloading

Defines multiple overloaded methods to calculate products of different data types. Supports operations for: Two integers,Two doubles,Integer and double in either order,Three integers or three doubles. Each method outputs the product result after performing the calculation.

Signup and view all the flashcards

Variable Scope in Math Operations

Defines various mathematical operations including sum, difference, product, circleCircumference, and circleArea. Prints out values of PI, and parameters within method scopes to demonstrate variable accessibility. Calculates circumference and area by invoking the respective formulas.

Signup and view all the flashcards

Study Notes

Rectangle Class

  • Rectangle Class Implementation: Contains attributes for width and height, methods for area and dimensions, and overrides toString() for a descriptive output.
  • RectangleTester Example: Tests Rectangle by creating an instance and printing its dimensions.

Method Calling in Rectangle Class

  • Getters for Rectangle Class: getHeight(), getWidth(), and getArea() methods return corresponding attributes.
  • RectangleTester Use: Creates a rectangle instance and prints its dimensions and area using the defined methods.

Point Class Usage

  • Point Class Features: Stores x and y coordinates, includes a method to move the point and overrides toString() to display position.
  • PointTester Example: Demonstrates creation, movement, and printing of Point objects.

Student Class Implementation

  • Attributes of Student Class: Contains firstName, lastName, and gradeLevel, and includes a toString() method for representation.
  • StudentTester Demonstration: Creates student instances and prints their information.

Text Message Class

  • TextMessage Class Design: Defines sender, receiver, and message, with a toString() method for formatted output.
  • Messages Example: Instantiates and prints messages between Bart and Lisa.

Coin Flip Simulation

  • CoinFlips Class: Simulates FLIPS number of coin tosses, counting heads and tails outcomes, and calculates percentages.
  • Randomizer Usage: Generates random boolean values for coin toss outcomes.

Longest Streak Tracker

  • LongestStreak Class: Tracks longest consecutive "Heads" results from flips, resetting streak upon a "Tails" outcome.
  • Implementation of Random Flips: Uses Randomizer for generating coin toss results.

Distance Calculation Between Locations

  • GeoLocation Class: Contains latitude and longitude data; includes method for distance calculation based on spherical law of cosines.
  • HowFarAway Program: Prompts user for location coordinates and calculates distance between two geographical points.

Triangle Class Details

  • Triangle Attributes: Initialized with width and height, includes toString() for formatted output.
  • TriangleTester Usage: Tests triangle creation and output representation.

Flower and Dog Class Implementations

  • Flower Class: Encapsulates flower details (name, color, genus, species) with a descriptive toString().
  • Dog Class: Represents dog breed and name, with a formatter in toString() to display information.

Student Class Enhancements

  • GPA Attribute Addition: Introduces a GPA field to student class with getter/setter methods and honors student determination.
  • StudentTester Expansion: Logs output for student GPA and honors status checks.

Pizza Class Construction

  • Pizza Object Design: Comprises type, toppings, and size with a formatted string representation.

Fraction Class Implementation

  • Fraction Class: Represents rational numbers with numerator and denominator; includes toString() and getter/setter methods.

Batting Average Calculation

  • BaseballPlayer Class: Stores player stats, includes a method to calculate batting average, and formatted output for player performance.

Static Player Management

  • Player Class: Maintains a static count of players, with a maximum limit and features construction to increase player count upon instantiation.

Unit Circle Calculation

  • UnitCircle Class: Computes cosine and sine for angles in radians, displaying results for each calculated angle.

Text Message Getter Methods

  • TextMessage Class: Includes getter methods for sender, receiver, and message content, facilitating information retrieval.

Fraction Getter/Setter Implementation

  • Fraction Class Enhancements: Adds methods to retrieve and modify numerator and denominator values, improving data encapsulation.### Game Full Function
  • gameFull() checks if the total number of players has reached the maximum limit.
  • Returns true if totalPlayers is greater than or equal to maxPlayers.

Circle Area Calculation

  • Class CircleTester tests the area calculation of circles with specific radii.
  • Creates a Circle object with radius 5 and prints its area.
  • Creates another Circle object with radius 12 and prints its area.
  • Class Circle defines a static constant PI for mathematical calculations.
  • getArea() method calculates the area using the formula: Area = Ï€ * radius².

Rock, Paper, Scissors Winner Logic

  • Method getWinner() determines the result between the user's choice and the computer's choice.
  • If both choices are the same, it returns "Tie".
  • Implements rules for winning: rock beats scissors, scissors beats paper, paper beats rock.

Rock, Paper, Scissors Game Implementation

  • Class RockPaperScissors facilitates interaction with the user through console inputs.
  • Utilizes constants for win messages and options (rock, paper, scissors).
  • Continues taking user input until a null value is provided.
  • Randomly generates the computer's choice using Randomizer.

Randomizer Class

  • Singleton design pattern ensures only one instance of the Random object exists.
  • Provides various random number generation methods:
    • nextBoolean(), nextInt(min, max), and nextDouble(min, max).
  • Allows customization of probability for boolean outcomes.

Product Method Overloading

  • Class Product defines multiple overloaded methods to calculate products of different data types.
  • Supports operations for:
    • Two integers
    • Two doubles
    • Integer and double in either order
    • Three integers or three doubles.
  • Each method outputs the product result after performing the calculation.

Variable Scope in Math Operations

  • Class MathOperations illustrates usage of local and instance variables.
  • Defines various mathematical operations including sum, difference, product, circleCircumference, and circleArea.
  • Prints out values of PI, and parameters within method scopes to demonstrate variable accessibility.
  • Calculates circumference and area by invoking the respective formulas.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

CS112 Assignment 2: Java Rectangle Class
6 questions
Geometry Class: Classifying Shapes
3 questions
C# Chapter 9: Rectangle Class Quiz
23 questions
Use Quizgecko on...
Browser
Browser