Computer Programming II (CS141)
Q.1.
Why would you use an inner class instead of regular class?
Q.2.
What does this code print? Why is it polymorphic?
DataSet data = new DataSet();
data.add(new BankAccount(1000));
data.add(new Coin(0.1, "dime"));
System.out.println(data.getAverage());
Q.3.
Can you convert a superclass reference into a subclass reference? A
subclass reference
into a superclass reference? If so, give examples. If not, explain
why not.
Q.4.
Implement a superclass Person. Make two classes, Student and
Instructor that inherit
from Person. A person has a name and a year of birth. A student has
a major, and an
instructor has a salary. Write the class declarations, the
constructors, and the methods
toString for all classes. Supply a test program that tests these
classes and methods.
Q.5.
Implement the compare method of the following class
RectangleComparator. The method compares two rectangles. The method
should return:
A positive integer if the area of the first rectangle is larger
than the area of the second rectangle
A negative integer if the area of the first rectangle is smaller
than the area of the second rectangle
0 if the two rectangles have the same area.
import java.util.Comparator;
import java.awt.Rectangle;
public class RectangleComparator implements
Comparator<Rectangle>
`{
/**
Compares two Rectangle objects.
@param r1 the first rectangle
@param r2 the second rectangle
@return 1 if the area of the first rectangle is larger than the
area of
the second rectangle, -1 if the area of the first rectangle is
smaller than the area of the second rectangle or 0 if the two
rectangles have the same area
*/
public int compare(Rectangle r1, Rectangle r2)
{
...
}
}
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Computer Programming II (CS141) Q.1. Why would you use an inner class instead of regular class?...
Language is JAVA.
Clarification for the Shape class (highlighted):
The following requirements specify what fields you are expected
to implement in your Shape class;
- A private String color that specifies the color of the
shape
- A private boolean filled that specifies whether the shape is
filled
- A private date (java.util.date) field dateCreated that
specifies the date the shape was created Your Shape class will
provide the following constructors;
- A two-arg constructor that creates a shape
with...
Java Programming
.zip or .jar with 4 files: Shape.java, Square.java,
TSquare.java, Triangle.java
In this homework, you will build the below hierarchy:
Overview:
You will implement the supplier code: Shape, Square, TSquare,
and Triangle, which will be used by the Homework7Main program. This
program will use the DrawingPanel to draw these objects onto a
canvas.
Specification:
You will take advantage of inheritance with using a super class,
Shape.java. (below) Shape is an abstract class, which means it can
be extended, but...
import math ''' Finish the code below as described. Use the completed class Square as an example. ''' class Square: ''' Each Square has a width and can calculate its area, its perimeter, and return a string representation of itself. ''' def __init__(self, width): ''' (float) -> None Create a new Square with the given width. ''' self.width = width def get_area(self): ''' () -> float Return this square's area. ''' return self.width*self.width def get_perimeter(self): ''' () -> float Return...
this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...
In Java please!
Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...
Registry Java Programming 2) Interface Comparator: The method for comparing two objects is written outside of the class of the objects to be sorted. Several methods can be written for comparing the objects according to different criteria. Specifically, write three classes, DescriptionComparator, FirstOccComparator, and LastOccComparator that implement the interface java.util.Comparator. DescriptionComparator implements the interface Comparator. The method compare compares the description of two objects. It returns a negative value if the description of the first object comes before the description...
JAVA You are given a class Critter which represents an animal. Critter class will be the superclass of a set of subclasses classes. A Critter has a weight and a position on a number line. The constructor initializes the position to 0 and sets the weight from the parameter. Critter has an ArrayList of Strings to keep a log of its activities. It has other methods which you can view here You are to implement the following subclasses of Critter...
Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...
Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...
CS 1102 Unit 5 – Programming Assignment In this assignment, you will again modify your Quiz program from the previous assignment. You will create an abstract class called "Question", modify "MultipleChoiceQuestion" to inherit from it, and add a new subclass of "Question" called "TrueFalseQuestion". This assignment will again involve cutting and pasting from existing classes. Because you are learning new features each week, you are retroactively applying those new features. In a typical programming project, you would start with the...