0% found this document useful (0 votes)
8 views6 pages

APCS Unit3&Unit4 If Loop Written

Uploaded by

gsin6555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

APCS Unit3&Unit4 If Loop Written

Uploaded by

gsin6555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assume that the classes listed in the Java Quick Reference have been imported where

appropriate.

• Unless otherwise noted in the question, assume that parameters in method calls are not
null and that methods are called only when their preconditions are satisfied.

• In writing solutions for each question, you may use any of the accessible methods that
are listed in classes defined in that question.

• Writing significant amounts of code that can be replaced by a call to one of these
methods will not receive full credit.

1. Write a method that takes a string as an argument returns the word reversed. For
example

public String reverse("oreo")

would return oero

Complete the method below.

public String reverse(String word){

}
2. Write a method that will produce a grid of numbers by taking 2 integers as arguments.
For example

public static void gridit(int r, int c) if r was 2 and c was 2 would produce

0001

1011

public static void gridit(int r, int c){

3. Write a method that will replace all instances of a letter in a word with. For example the
method

pubic static String replacelt("Hello World", "I") would produce

He**o Wor*d

public static String replacelt(String word, String let){

}
4. Create a method that will replace the number of letters in a word with random numbers.

For example:

pubic static String codelt("USA") would produce 957

public static String codelt(String word){

5. Write a while loop that will count down by 2 from 100 until it gets to 0.
6. Write the loop from #5 as a for loop.

7. Write a nested for loop that will produce the pattern below.

XY XY XY XY

XY XY XY XY

XY XY XY XY

8. How many times will the following loop iterate?

int x = 20;
while (x>0){
System.out.print(x);
x=3;
}
SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN
IN JAVA.
1. Write a method that takes a string and an integer as an arguments and prints a new string splitting the
word based on the position of the integer passed and reversing the two parts so that the second part is
first and the first part is second. For example if the word Aquinas was passed with the integer 3 the
following would be printed out: inasAqu (split on the 3rd letter as the index and flipped)

public void flipit( , ){

2. Write a method that will take 2 strings as arguments. The first string will be a word and the second
string will be a letter(if you want to make it a char that is fine but your code must work with it). The
method will then count the occurrence of the letter in the work and print it the number of times the letter
appears in the word. For example if you passed Galat and a the method would produce 2.

Public void countIt( , ){


3. Write a for loop that will count up from 0 to 50 printing each value on a separate line starting at 0
and printing 50.

4. Write a code segment that will do the the same as question #3 but as a while loop.

5. Using nested for loops (at least 2 loops) create the following pattern.
XXXXXX
XXXXXX
XXXXXX

6. How many time will the following loop iterate? Answer:


for (int n = 10; n<=30; n+=5){
System.out.print(n);
}

You might also like