1: package nnloops; 2: 3: import java.util.Scanner; 4: 5: public class NNLoops { 6: 7: NNLoops() 8: { 9: //oneToTen(); 10: //oddNumbers(); 11: //squareNumbers(); 12: //random4(); 13: //even(20); 14: //powers(8); 15: //areWeThereYet(); 16: //triangle(); 17: //tableSquare(); 18: tableSquares(6); 19: 20: } 21: 22: void oneToTen() 23: { 24: for(int i=1; i <11; i++) 25: { 26: System.out.println(i); 27: } 28: } 29: 30: void oddNumbers() 31: { 32: for (int i=1; i<20; i+=2) 33: { 34: System.out.println(i); 35: } 36: } 37: 38: void squareNumbers() 39: { 40: for (int i=1; i<11; i++) 41: { 42: System.out.println(i*i); 43: } 44: } 45: 46: void random4() 47: { 48: for (int i=0; i<4; i++) 49: { 50: System.out.println((int)(Math.random()*10)+1); 51: } 52: } 53: 54: void even(int n) 55: { 56: for (int i=1; i<n; i+=2) 57: { 58: System.out.println(i); 59: } 60: } 61: 62: void powers (int n) 63: { 64: for (int i =1; i<=n; i++) 65: { 66: System.out.println(Math.pow(2, i)); 67: } 68: } 69: 70: void areWeThereYet() 71: { 72: Scanner scan = new Scanner(System.in); 73: do 74: { 75: System.out.println("Are we there yet?"); 76: } while(!scan.nextLine().equals("Yes")); 77: System.out.println("Good!"); 78: } 79: 80: void triangle() 81: { 82: for(int i = 0; i<6; i++) 83: { 84: for (int j= 0; j<i; j++) 85: { 86: System.out.print("*"); 87: } 88: System.out.println(""); 89: } 90: } 91: 92: void tableSquare() 93: { 94: for(int i = 1; i<=4; i++) 95: { 96: for (int j=1; j<=4; j++) 97: { 98: System.out.print("|"+ i*j +"\t"); 99: } 100: System.out.println("|"); 101: 102: } 103: } 104: 105: void tableSquares(int n) 106: { 107: for(int i = 1; i<=n; i++) 108: { 109: for (int j=1; j<=n; j++) 110: { 111: System.out.print("|"+ i*j +"\t"); 112: } 113: System.out.println("|"); 114: 115: } 116: } 117: 118: 119: public static void main(String[] args) { 120: new NNLoops(); 121: } 122: }
Tag Archives: 99java
2: Loops
2.1 1 to 10
Write a method that prints the numbers 1 to 10
2.1.1 Example
1: oneToTen() 2: *** Output *** 3: 1 4: 2 5: 3 6: 4 7: 5 8: 6 9: 7 10: 8 11: 9 12: 10
2.2 Odd Numbers
Write a method that prints the positive odd numbers less than 20
2.2.1 Example
1: oddNumbers() 2: *** Output *** 3: 1 4: 3 5: 5 6: 7 7: 9 8: 11 9: 13 10: 15 11: 17 12: 19
2.3 Square Numbers
Write a method that prints the square numbers up to 100
2.3.1 Example
1: squares() 2: *** Output *** 3: 1 4: 4 5: 9 6: 16 7: 25 8: 36 9: 49 10: 64 11: 81 12: 100
2.4 Random Numbers
Write a for loop to print out four random integers between 1 and 10
2.4.1 Example
1: random4() 2: *** Output *** 3: 3 4: 5 5: 2 6: 8
2.5 Even Numbers < n
Write a method to print out the positive even numbers less than n
2.5.1 Example
1: even(20) 2: *** Output *** 3: 2 4: 4 5: 6 6: 8 7: 10 8: 12 9: 14 10: 16 11: 18
2.6 Powers of 2
Write a method to print out the powers of 2 from 21 up to 2n
2.6.1 Example
1: powers(8) 2: *** Output *** 3: 2 4: 4 5: 8 6: 16 7: 32 8: 64 9: 128 10: 256
2.7 Are we there yet?
Write a program that outputs “Are we there yet?” and then waits for input. If the input is “Yes” the program outputs “Good!” and exits, otherwise the program loops.
2.7.1 Example
1: "Are we there yet?" 2: No 3: "Are we there yet?" 4: Spoons 5: "Are we there yet?" 6: Yes 7: Good!
2.8 Triangle
Write a method that uses nested loops to produce the following pattern
1: triangle() 2: *** Output *** 3: * 4: ** 5: *** 6: **** 7: *****
2.9 Table Square
Write a method that prints out a 4 x 4 table square
2.9.1 Example
1: tableSquare() 2: *** Output *** 3: A 4 x 4 table square 4: | 1 | 2 | 3 | 4 | 5: | 1 | 2 | 3 | 4 | 6: | 2 | 4 | 6 | 8 | 7: | 3 | 6 | 9 | 12 | 8: | 4 | 8 | 12 | 16 |
2.10 Table Squares
Extend your answer to the last question produce a method that will print out a n x n table square
2.10.1 Example
1: tableSquares(6) 2: *** Output *** 3: A 6 x 6 table square 4: | 1 | 2 | 3 | 4 | 5 | 6 | 5: | 2 | 4 | 6 | 8 | 10 | 12 | 6: | 3 | 6 | 9 | 12 | 15 | 18 | 7: | 4 | 8 | 12 | 16 | 20 | 24 | 8: | 5 | 10 | 15 | 20 | 25 | 30 | 9: | 6 | 12 | 18 | 24 | 30 | 36 |
1: How to Answer These Questions
Write methods to solve all of the questions. Here are two example questions and their solutions. Notice that the first method prints a value, the second method returns a value.
1.1 Hello <Name>
Write a method that accepts a name as a parameter and prints out “Hello ” <name>
1.1.1 Example
1: hello("Kim") 2: *** Output *** 3: Hello Kim
1.2 Average of two numbers
Write a method that accepts two numbers and returns the average of the two numbers.
1.2.1 Example
1: System.out.println(average(3,4)); 2: *** Output *** 3: 3.5
1.3 Solutions
1: public class NinetyNine { 2: 3: NinetyNine() 4: { 5: hello("Kim"); 6: System.out.println(average(3,4)); 7: } 8: 9: void hello(String s) 10: { 11: System.out.println("Hello " + s); 12: } 13: 14: double average(double x, double y) 15: { 16: return (x+y)/2; 17: } 18: 19: public static void main(String[] args) 20: { 21: new NinetyNine(); 22: } 23: } 24: