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

Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview Th

ID: 665843 • Letter: O

Question

Objectives

Problem solving using arrays and ArrayLists. Abstraction.

Overview

The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included):

TRIPLE                      as in block-letter M

DOUBLE                   as in block-letters U, M, A

LEFT_DOT                as in block-letters F, S

CENTER_DOT          as in block-letter I

RIGHT_DOT as in block-letter S

You must complete implementations of classes LineSegment, BlockLetter, andBanner.

Specific Requirements

The enum type SegmentType is already implemented. Its values may be referenced as SegmentType.TRIPLE, SegmentType.CENTER_DOT, etc.

2.The LineSegment class represents a line-segment of one of the 6 types

Instance variable: a String of space and dot characters

Implement public methods: length() and toString().

Implement a constructor helper method for each different type ofLineSegment.

3.The BlockLetter class represents an uppercase block-letter

Instance variable: an array of LineSegment elements.

Implement public methods: size(), toString() and getSegment(int index).

Method getSegment(int index) returns a LineSegment element selected byindex.

Implement a constructor helper for letters: A, C, E, F, H, I, L, M, O, P, S, T, U, W

The (default) implementation of any unrepresented character is a blurb -- aBlockLetter with all LineSegments of type SegmentType.SOLID, and using the character itself as the print-symbol of its LineSegments. (R in the diagram above)

4.The Banner class represents a character string in block-letters

Instance variable: an ArrayList of BlockLetter elements.

Implement the constructors and the toString() method only.

BANNERCLIENT:

public class BannerClient
{
public static void main(String[] args)
{
System.out.println( new Banner("MIAMI RULES", 7) );
System.out.println();
System.out.println( new Banner("FIU PANTHERS", 11, 'x') );
}
}

BANNER CLASS:

import java.util.*;
import javax.swing.*;
public class Banner
{
//Instance Variable - A collection of BlockLetter elements
private ArrayList letters;

//Constructor
// @param words : The content of this Banner
// @param height: Height of the letters in this Banner
// default is to construct each letter using the letter
// itself as the print-symbol in each BlockLetter
public Banner(String words, int height)
{
}

//Constructor
// @param words : The content of this Banner
// @param height: Height of the letters in this Banner
// @param symbol: Print-symbol used in all BlockLetter s
public Banner(String words, int height, char symbol)
{
}

public String toString()
{
String image = "";
return image;
}
}

BLOCKLETTER:

//An instance of this class represents a block letter
//Full BlockLetter representations are provided for
// A, C, E, F, H, I, L, M, O, P, S, T, U, W
//All other characters are represented as "blurbs",
// solid lines composed of the character
public class BlockLetter
{
public static final int MIN_SIZE = LineSegment.MIN_WIDTH;

//Instance Variable: a collection of LineSegment elements
private LineSegment[] block;

//Constructor
// @param letter: the character represented by this BlockLetter
// default size : MIN_SIZE
// default print_symbol : the letter
public BlockLetter(char letter)
{
this(letter, MIN_SIZE);
}

//Constructor
// @param letter: the character represented by this BlockLetter
// @param size : size of this BlockLetter, odd number >= MIN_SIZE
// default print_symbol : the letter
public BlockLetter(char letter, int size)
{
this(letter, size, letter);
}

//Constructor
// @param letter: the character represented by this BlockLetter
// @param size : size of this BlockLetter, odd number >= MIN_SIZE
// @param symbol: the "dot"/character used in the LineSegments
public BlockLetter(char letter, int size, char symbol)
{
if (size < MIN_SIZE)
throw new RuntimeException("Minimum Size is " + MIN_SIZE);

if (size % 2 == 0)
size = size + 1;
  
switch ( letter )
{
default : blurb(letter);
}
}

//Accessor - returns a selected LineSegment of this BlockLetter
// @param index: 0 .. this.size()-1, selects a LineSegment
public LineSegment getSegment(int index)
{
return null;
}

//Returns the number of LineSegments in this BlockLetter
public int size()
{
return 0;
}

//Override - returns a printable image of this BlockLetter
public String toString()
{
return "";
}
  
private void blurb(char symbol)
{
}

//*************************************************************
// Throw-away tester
public static void main(String[] args)
{
String chars = "ACEFHILMOPSTUWX";
for (int i = 0; i < chars.length(); i++)
System.out.println( new BlockLetter(chars.charAt(i))
+ " " );
}
}

LINESEGMENT:

public class LineSegment
{
public final static int MIN_WIDTH = 5;

//Instance Variable
private final String segment;

//Constructor - create a LineSegment of some given SegmentType
// @param type: the type of this LineSegment
// @param width: the width of this LineSegment, an odd integer
// @param dot: the character used in constructing this LineSegment
// RuntimeExcption thrown if the requested width is even or < MIN_WIDTH
public LineSegment(SegmentType type, int length, char dot)
{
if (length < MIN_WIDTH || length % 2 == 0)
throw new RuntimeException("Width must be odd, >= " + MIN_WIDTH);

switch ( type )
{
case SOLID : segment = solid(length, dot); break;
case TRIPLE : segment = triple(length, dot); break;
case DOUBLE : segment = doubles(length, dot); break;
case LEFT_DOT : segment = leftDot(length, dot); break;
case CENTER_DOT: segment = centerDot(length, dot); break;
case RIGHT_DOT : segment = rightDot(length, dot); break;
default : segment = "";
}
}

//Return the length (width) of this LineSegment
public int length()
{
return 0;
}

//Override - returns a printable image of this LineSegment
public String toString()
{
return "";
}

//*************************CONSTRUCTOR HELPERS***********************

//Return a SegmentType.SOLID LineSegment
// @param width: The width/length of this SOLID LineSegment
// @param dot: The symbol used in constructing this LineSegment
private String solid(int width, char dot)
{
return "";
}

//Return a SegmentType.TRIPLE LineSegment
// @param width: The width/length of this LineSegment
// @param dot: The symbol used in constructing this LineSegment
private String triple(int width, char dot)
{
return "";
}

//Return a SegmentType.DOUBLE LineSegment
// @param width: The width/length of this LineSegment
// @param dot: The symbol used in constructing this LineSegment
private String doubles(int width, char dot)
{
return "";
}

//Return a SegmentType.LEFT_DOT LineSegment
// @param width: The width/length of this LineSegment
// @param dot: The symbol used in constructing this LineSegment
private String leftDot(int width, char dot)
{
return "";
}

//Return a SegmentType.CENTER_DOT LineSegment
// @param width: The width/length of this LineSegment
// @param dot: The symbol used in constructing this LineSegment
private String centerDot(int width, char dot)
{
return "";
}

//Return a SegmentType.RIGHT_DOT LineSegment
// @param width: The width/length of this LineSegment
// @param dot: The symbol used in constructing this LineSegment
private String rightDot(int width, char dot)
{
return "";
}
}

SEGMENTTYPE:

public enum SegmentType
{
//Width 7 Examples
SOLID, // *******
TRIPLE, // * * *
DOUBLE, // * *
LEFT_DOT, // *
CENTER_DOT, // *   
RIGHT_DOT; // *
}

Explanation / Answer

public class LineSegment
{
public static final int SOLID = 0;
public static final int LEFT_END = 1;

private int style;
private int width;
private char symbol;
private String segment;

public LineSegment(int style, int width, char symbol)
{
this.style = style;
this.width = width;
this.symbol = symbol;
switch(style)
{
case 1 : makeLEFT_ENDSegment(); break;
default : makeSOLIDSegment();
}
}
private void makeSOLIDSegment()
{
this.segment = "";
for(int k=0;k<=width;k++)
segment += symbol;
}
private void makeLEFT_ENDSegment()
{
segment = "" + symbol;
for(int k=1;k<=width-1;k++)
segment += "";
}
public String toString()
{
return segment;
}
}

public class BlockLetter
{
private LineSegment[]block;
private char letter;
private char symbol;
private int size;

public BlockLetter(char letter, char symbol, int size)
{
this.letter=letter;
this.symbol = symbol;
this.size = size;
this.block=new LineSegment[size];
switch( letter )
{
case 'L' : build_L(); break;
default : buildBlockLetter();
}
}
private void build_L()
{
for(int row=0;row<this.size-1;row++)
block[row] = new LineSegment(LineSegment.LEFT_E...
block[block.length-1] = new LineSegment(LineSegment.SOLID,...
}
public LineSegment getSegment(int row)
{
return block[row];
}

private void buildBlockLetter()
{
for(int row=0; row<this.size; row++)
{
this.block[row] = new LineSegment(LineSegment.SOLID,...
}
}
public String toString()
{
??;
}
}

public class BlockLetterTest
{
public static void main(String args[])
{
System.out.println( new BlockLetter('L', '*', 7) );
}
}