



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An assignment for a computer science i course focused on writing for loops and printing patterns using java. The assignment consists of seven parts, each requiring the submission of a separate java program. Students are expected to define integer values, write for loops, and use the fang game canvas to display results. Proper indentation, whitespace, and comments are required for full credit.
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!
In this assignment, you will have the opportunity to practice writing for loops. In the last part, you will use the FANG game canvas to display your results. The assignment has several parts. For each part, you will write and submit a separate Java program. Every program you submit must have an ID block at the top giving the assignment name and your name (as author), in the standard ID block format. For full credit, your program must be indented properly with good use of whitespace and comments.
Each of the programs you will submit will require that you specify one or more integer values that will determine the size or shape of your output. Since we don’t have an easy way to enter an integer value from the keyboard (yet), you can simply define an integer value using something like this (inside the the body of the class):
private static final int LINES = 5;
In all but the last part of this assignment, you will define a main method that will solve the particular problem statement. In your main method, you can use the LINES variable to print your results.
Each of your submissions for this assignment will look like this, with some variations.
/**
public static final int LINES = 5;
public static void main(String [] args) { // your program code goes here } }
Here we have shown the start of your solution to problem number 1, which you would save in a file called Prob1.java. Of course, for other problems, you will replace the 1 in the class and file names with the appropriate problem number. Your header block should contain the usual information that we described in Lab 2. The LINES variable is used in program Prob1.java. You will be using other variables in later problems.
Write a program that will produce the following output, using a for loop and a variable LINES defined to be 5. You should have exactly one System.out.println statement and one for loop that iterates LINES times. Your loop should be constructed so that if LINES is defined to be 7 instead of 5, there will be 7 lines of output. You can use System.out.println("*******") to print each line.
Use moodle to submit your Prob1.java code for this part of the assignment. (Do the same for each of the following problems as well.)
Write a program Prob2.java that will produce the same 5-line output, but instead of using System.out.println("*******") to print each line, you should instead write a loop that will do System.out.print("*") seven times. Create a variable STARS defined to be 7, as we did for LINES defined to be 5, so that the number of stars to be printed can be changed easily. After loop that prints the seven stars, you will need to do System.out.println() to move to the next line. In summary, this program will have two (nested) for loops, one System.out.print, and one System.out.println. Your program should work for any reasonable values of LINES and STARS.
Create a program Prob3.java that will produce the following 5-line output. Use the Prob2 program as a starting point.
**
Use the LINES variable as before, but the number of stars that get printed on a line will increase by one from line to line: the number of times your inner for loop will execute will depend on the value of the variable you used in your outer for loop. This program should work for any reasonable value of LINES.
Create a program Prob4.java that will print the same output as Prob3 except with the line number preced- ing each line you print, followed by a single space, as in the following:
1 * 2 ** 3 ***
Here you may find it convenient to define a method stuff with the signature given here. Also, for con- venience, you should define a method newline that simply goes to a new line. Use the following code template:
public static void stuff(int n, String s) { // print n copies the String s on the current line }
public static void newline() { // go to a new line }
To print seven plus signs (+) followed by a new line, you would do
stuff(7, "+"); newline(); Your program should work for any reasonable value of SIZE.
Write a FANG-based program Circles.java that will display circles on the game canvas, using a variable CIRCLES that is initially defined to be 3. In this case, you will use a definition for CIRCLES almost identical to the definition of LINES in your previous programs, except you will need to remove the static modifier. (We will see why this is the case later in the course.) Here is what your output should look like with CIRCLES equal to 3:
Here is a template for starting your program:
import fang.core.Game; import fang.sprites.OvalSprite;
public class Circles extends Game {
private final int CIRCLES = 3;
public void setup() { // create your circles here } }
Your program should produce the pattern shown above for any reasonable value of CIRCLES. The centers of each of the circles should have y -coordinate 0.5. The first circle will have diameter 0.5, the second will have diameter 0.25, the third will have diamter 0.125, and so forth – each new diameter will be half of the previous diameter. In your setup method, you will write a for loop that will create each of the circles – make the color of the circle whatever you’d like – and add it to the canvas. Since there will be no restrictions on the value of CIRCLES, your circles will march closer and closer to the right-hand side of the canvas. (Question: will they ever reach the right-hand side of the canvas?) You’ll need to determine a formula for the x -coordinate of each new circle, which involves the x - coordinate of the previous circle and the next circle’s diameter. Here is some code to start out with:
double diam = 0.5; // diameter of the first circle double x = diam/2; // x-coordinate of the first circle for ( ... ) { OvalSprite cir = new OvalSprite(diam, diam); cir.setColor(getColor(...)); // your choice! cir.setLocation(x, 0.5); addSprite(cir); diam = diam/2; // diam of next circle x = ...; // new x-coordinate of next circle }
When you have a debugged and running program, submit it through moodle with the name Circles.java.
Your CCirles.java program should display circles within circles on the game canvas. Here is what it should look like with CIRCLES equal to 3: