Programming Problem #1

Write 2 Classes:

The first class:

  • A class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient:
    • public Letter(String from, String to)
  • Supply a method
    • public void addLine(String line)
    • to add the line of text passed as a parameter to the body of the letter
    • the method should also add an additional newline character.
  • Supply a method
    • public String getText()
    • that returns the entire text of the letter. The text has the form
Dear recipient name:
first line of the body
second line of the body
. . .
last line of the body
Sincerely,
sender name

The second class

  • Also supply a class LetterPrinter that has a main method that prints this letter to the console:
Dear John:
I am sorry we must part.
I wish you all the best.
Sincerely,
Mary
  • It should use first class by
    • constructing an object of the Letter class
    • and calling addLine twice
    • and then outputting the letter

Hints:

Use the concat method or the + operator to form a longer string from two shorter strings.

The special string "n" represents a new line. For example, the statement

body = body.concat("Sincerely,").concat("(backslash)n");

Programming Problem #2

Write a program with 3 classes that draws a picture of a house. It could be as simple as the accompanying figure, or if you like, more elaborate (3-D, skyscraper, marble columns in the entryway, whatever). Implement a class House and supply a method draw(Graphics2D g2) that draws the house. It should fit in a 400 by 400 window. Bonus points for the best house. Upload your House.java file. Here are two of the classes that you should leverage:

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
This component draws a house
*/
public class HouseComponent extends JComponent{
private static final long serialVersionUID = 5368385930392743141L;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
House house = new House();
house.draw(g2);
}
}
import javax.swing.JFrame;
public class HouseViewer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setTitle("House");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HouseComponent component = new HouseComponent();
frame.add(component);
frame.setVisible(true);
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.