8A
Create a class Liners
representing a series of horizontal lines. The constructor takes three parameters: The x and y coordinates of the left-hand side of the first line and the number of lines. The length of the first line is 20 times the number of lines. Each successive line is 15 pixels shorter than the one before. The lines are spaced 5 pixels apart. Define and use constants.
Provide a draw method that draws the pattern of lines. You must use a loop.
public Liners(int x, int y, int n)
Do not draw in the constructor.public void draw()
Provide Javadoc
For the draft: Provide the constructor and the instance variables. In the draw method, draw the first line of the correct length
Your final will look like this
8B
Write a class StringsAgain
whose constructor takes a String parameter. StringsAgain
has the following methods:
public String getWord()
which gets the wordpublic void setWord(String s)
sets a new String. public String reverseLastThree()
gets a String in which the last three characters are in reverse order and the others are unchanged. If there are less than three characters in the string, return the string unchanged. Use a loop to reverse the characters. Then if I asked you to reverse the last 5, you would only need to change a few numbers.public String mostFrequent()
which returns the letter which appears most often in the String. If two letters have the most occurrences, return the first. Consider upper and lower case as the same letter. Return the lowercase character. You will need nested loops and will have to iterate through the String multiple times.Provide Javadoc for the class, the constructor and all methods
For the draft: Provide instance variables. Implement the constructor. Implement the first two methods completely. Implement the last two methods as stubs. Provide all the Javadoc. For the stubs, document what the method will do in the final - not what it does in the draft
8C
In this problem you write a PatchworkQuilt
class which will model a simple patchwork quilt. The quilt consists of 8 rows of squares. Each row contains 7 squares. Each square is a random Pattern constructed from the Pattern
class which is provided. Here are a couple of image of a patch work quilts, in case you are not familiar with them.
You can look at the Javadoc for the Pattern
class by selecting it in the Bluej Editor and then selecting Documentation in the drop-down menu on the right. I have also included a PatternTester
class so you have an example of how to use the class. You can download both from Codecheck. Notice that Codecheck reports an error with PatternTester. Just ignore it. If your code is correct, you will still pass Codecheck with 1/1. It will run correctly in Bluej.
Write the PatchworkQuilt
class. You must use nested loops. Draw the rows and then the columns to keep Codecheck happy. Use the constants provided.
The PatchworkQuilt
will have this constructor and these methods
public PatchworkQuilt(int x, int y)
where x and y are the coordinates of the upper left-hand corner of the quilt. You will need to declare an instance variable of type Random and then in the constructor create a Random object with a seed of 2016. See below. Do not draw in the constructor.
public class PatchworkQuilt { private Random gen; /** * Constructor for objects of class PatchworkQuilt */public PatchworkQuilt(int theX, int theY) { gen = new Random(2016); //more stuff here } }
public void draw()
draws the quilt with the 8 rows of 7 of squares patterns. Use a nested loopprivate Pattern getPattern()
randomly constructs one of the four Pattern types and returns the Pattern object.. Use the Random object created for you in the constructor to determine which to return. The version of getPattern
that you need for the draft is provided below. (You will change it in the final)
/** * Gets one of the Patterns * @return a random Pattern */ public Pattern getPattern() { return new Pattern(Pattern.BLUE_CIRCLE); }
The Pattern class has 4 different pattern types. To determine which pattern to return in getPattern
method you will generate a random number between 0 (inclusive) and 4 (exclusive) and then construct a Pattern with that parameter Pattern myPattern = new Pattern(gen.nextInt(4));
In PatchworkQuilt's
draw method, you will create a Pattern
and use its translate
method to move it to the correct position. Use the constant Pattern.SIDE
(the width and height of the square) to position the square.
You will need to import the graphics package into Bluej.
Because the Random object is created with a seed, you will all get the same drawing every time. That is so Codecheck can check it.
For the draft, complete the constructor to initialize the instance variables. Write a draw method that draws the first row starting at the given (x, y) coordinates. (This will become the inner loop of the final. Call getPattern
() to get the squares to use. Right now getPattern
always returns BLUE_CIRCLE square. (You will change that in the final) You must use a loop. You will not receive credit for simply drawing 8 squares.
Your final output will look like this