-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectCh5App.java
More file actions
108 lines (94 loc) · 2.81 KB
/
ProjectCh5App.java
File metadata and controls
108 lines (94 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gr.aueb.cf.ch10.projects;
import java.util.Scanner;
public class ProjectCh5App {
final static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
boolean quit = false;
String response;
do {
printMenu();
response = getChoice();
try {
if (response.matches("[qQ]")) {
quit = true;
} else {
printOnChoice(response);
}
} catch (IllegalArgumentException e) {
System.out.println("Invalid Choice");
}
} while (!quit);
}
public static void printOnChoice(String s) throws IllegalArgumentException {
int choice;
int n =0;
try {
choice = Integer.parseInt(s);
if ((choice >= 1) && (choice <= 5)) {
System.out.println("Please insert the number of stars");
n = Integer.parseInt(getChoice());
}
switch (choice) {
case 1:
printStarsH(n);
break;
case 2:
printStarsV(n);
break;
case 3:
printHV(n);
break;
case 4:
printHVAsc(n);
break;
case 5:
printHVDesc(n);
break;
default:
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw e;
}
}
public static void printMenu(){
System.out.println("Please select on of the following: ");
System.out.println("1. Print H ");
System.out.println("2. Print V ");
System.out.println("3. Print HV ");
System.out.println("4. Print HV Asc ");
System.out.println("5. Print HV Desc ");
}
public static String getChoice() {
return in.nextLine().trim();
}
public static void printStarsH (int n) {
for (int i = 1; i <= n; i++) {
System.out.print("*");
}
}
public static void printStarsV (int n) {
for (int i = 1; i <= n; i++) {
System.out.println("*");
}
}
public static void printHV (int n) {
for (int i = 1; i <= n; i++) {
printStarsH(n);
System.out.println();
}
}
public static void printHVAsc (int n) {
for (int i = 1; i <= n; i++) {
printStarsH(i);
System.out.println();
}
}
public static void printHVDesc (int n) {
for (int i = 1; i >= n; i--) {
printStarsH(i);
System.out.println();
}
}
}