The goal of this programming assignment is to practice implementing a class in J
ID: 3889890 • Letter: T
Question
The goal of this programming assignment is to practice implementing a class in Java, using access modifiers, and developing your algorithms for a problem.
- Your program is going to draw 5 different triangles, (1) left-bottom right triangle, (2) right-bottom right triangle, (3) left-top right triangle, (4) right-top right triangle, (5) isosceles triangle with fixed height
- Implement the 5 print functions
- Update access modifiers of these 5 print functions
- For the isosceles triangle, since you know the height, first you need to compute the base, and then, start drawing.
HERE IS THE FRAMEWORK REQUIRED:
public class Assignment1 {
private final int HEIGHT = 11;
public enum Patterns{
bottomLeftTriangle,
bottomRightTriangle,
topLeftTriangle,
topRightTriangle,
centerTriangle,};
// Write details of your algorithm
void printCenter(char ch){}
// Write details of your algorithm
void printBottomLeft(char ch){}
// Write details of your algorithm
void printTopLeft(char ch){}
// Write details of your algorithm
void printBottomRight(char ch){}
// Write details of your algorithm
void printTopRight(char ch){}
public void printPattern(Patterns patternId, char ch){
switch (patternId){
case bottomLeftTriangle:
this.printBottomLeft(ch);
break;
case bottomRightTriangle:
this.printBottomRight(ch);
break;
case topLeftTriangle:
this.printTopLeft(ch);
break;
case topRightTriangle:
this.printTopRight(ch);
break;
case centerTriangle:
this.printCenter(ch);
break;
default:
this.printTopLeft(ch);
break;
}
}
public static void main(String[] args) {
Assignment1 ass1 = new Assignment1();
ass1.printPattern(Assignment1.Patterns.bottomLeftTriangle, '*');
ass1.printPattern(Assignment1.Patterns.bottomRightTriangle, '#');
ass1.printPattern(Assignment1.Patterns.topLeftTriangle, '@');
ass1.printPattern(Assignment1.Patterns.topRightTriangle, '%');
ass1.printPattern(Assignment1.Patterns.centerTriangle, '^');
}
}
Explanation / Answer
Assignment1.java
public class Assignment1 {
private final int HEIGHT = 11;
public enum Patterns{
bottomLeftTriangle,
bottomRightTriangle,
topLeftTriangle,
topRightTriangle,
centerTriangle,};
// Write details of your algorithm
void printCenter(char ch)
{
for (int i = 1; i <= HEIGHT; i++) {
if (i % 2 != 0) {
for (int l = 0; l <= ((HEIGHT + 1) - i); l++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("^ ");
}
for (int g = 0; g <= ((HEIGHT + 3) - i); g++) {
System.out.print(" ");
}
System.out.println();
}
}
}
// Write details of your algorithm
void printBottomLeft(char ch)
{
for(int i=1;i<=HEIGHT;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(ch);
}
System.out.println();
}
}
// Write details of your algorithm
void printTopLeft(char ch)
{
for (int i = HEIGHT; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(ch);
}
System.out.println(" ");
}
}
// Write details of your algorithm
void printBottomRight(char ch)
{
for (int i = HEIGHT; i >= 1; i--)
{
for (int j = 1; j <= HEIGHT; j++)
{
if (i > j)
System.out.print(" ");
else
System.out.print(ch);
}
System.out.println(" ");
}
}
// Write details of your algorithm
void printTopRight(char ch)
{
for (int i = 1; i <=HEIGHT; i++)
{
for (int j = 1; j <=HEIGHT; j++)
{
if (i < j)
System.out.print(ch);
else
System.out.print(" ");
}
System.out.println();
}
}
public void printPattern(Patterns patternId, char ch){
switch (patternId){
case bottomLeftTriangle:
this.printBottomLeft(ch);
break;
case bottomRightTriangle:
this.printBottomRight(ch);
break;
case topLeftTriangle:
this.printTopLeft(ch);
break;
case topRightTriangle:
this.printTopRight(ch);
break;
case centerTriangle:
this.printCenter(ch);
break;
default:
this.printTopLeft(ch);
break;
}
}
public static void main(String[] args) {
Assignment1 ass1 = new Assignment1();
ass1.printPattern(Assignment1.Patterns.bottomLeftTriangle, '*');
ass1.printPattern(Assignment1.Patterns.bottomRightTriangle, '#');
ass1.printPattern(Assignment1.Patterns.topLeftTriangle, '@');
ass1.printPattern(Assignment1.Patterns.topRightTriangle, '%');
ass1.printPattern(Assignment1.Patterns.centerTriangle, '^');
}
}
_____________________
Output:
*
**
***
****
*****
******
*******
********
*********
**********
***********
#
##
###
####
#####
######
#######
########
#########
##########
###########
@@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@
@@@@@@@@
@@@@@@@
@@@@@@
@@@@@
@@@@
@@@
@@
@
%%%%%%%%%%
%%%%%%%%%
%%%%%%%%
%%%%%%%
%%%%%%
%%%%%
%%%%
%%%
%%
%
^
^ ^ ^
^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.