How can i create these shapes with java - java

package assignment.pkg3;
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("What integer length do you want: ");
int length = stdIn.nextInt();
for (int i = 0; i < length; i++) {
for (int j = ((length - 1) - i); j > 0; j--) {
System.out.print(" ");
}
for (int a = 0; a <= i; a++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int a = length; a > i; a--) {
System.out.print("*");
}
for (int b = (length - 1); b > i; b--) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i < length; i++) {
for (int j = 0; j <= i; j++) {
for (int k = 0; k < i; k++) {
System.out.print(" ");
}
for (int a = 0; a <= i; a++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
Hi, I need help making this 3rd shape.. I don't even know how to start it. I did the other 3 pretty well. (except I have the spaces wrong on shape D). Can anyone help me find somewhere to start. (BTW I'm in a beginner java class so I can't put anything in there that we haven't learned. So sticking to for loops is basically all we can essentially use lol)
Here is link to pic

Try to execute this one & check -
for (int i = 0; i < length/2; i++){
for (int j = 0; j < length; j++){
System.out.print("*" + " ");
}
System.out.println("");
for (int k = 0; k < length-1; k++){
System.out.print(" " + "*");
}
System.out.println("");
}
Hope this will solve your problem.

Related

Spacing before Christmas tree trunk

I have an exercise where I have to print a christmas tree.
public class ChristmasTree {
public static void main(String[] args) {
int size = 6;
printChristmasTree(size);
}
public static void printChristmasTree(int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < (2 * i + 1); j++) {
System.out.print("*");
}
System.out.println();
}
for (int k = 0; k < 1; k++) {
System.out.print("**");
System.out.println();
}
System.out.println();
}
}
What I get is:
*
***
*****
*******
*********
***********
**
How can I fix it? How should I change this loop to move that trunk to the center of that tree?
Change
for (int k = 0; k < 1; k++) {
System.out.print("**");
System.out.println();
}
to
for (int k = 0; k < size; k++) {
System.out.print(" ");
}
System.out.print("**");
You need to loop to find number of spaces from size, and place it accordingly. Adjust the loop variable k if it is off a bit.
You said christmass tree so I guessed that you want the tree trunk to be in different lines. If it's what you want it's the code here:
for (int k = 0; k < 2; k++) {
for (int l = 0; l <= size; l++) {
if (l != size)
System.out.print(" ");
else
System.out.println("*");
}
System.out.println();
}
If you want in the same line here you are:
for (int l = 0; l <= size; l++) {
if (l != size)
System.out.print(" ");
else
System.out.print("**");
}
System.out.println();

How do I print the same output multiple times, horizontally and vertically, in a grid?

I can print a single pine tree of varying sizes, but how do I print an exact replica of it next to it and underneath it, in a grid, using only for loops? This project utilizes a scanner so that the User can vary the size and number of section each tree has. The last portion of the project allows the user to pick how many trees to print, horizontally(next to each other) and vertically(underneath it). The exact wording and an example: Program makes use of a third input to print a number of trees. Ex: When NUM_TREES is three, a 3x3 grid of trees is printed. Any tips or hints are helpful, thank you.
Here is the code I have to get a single tree:
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
printTree();
printStem();
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.println("\\");
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++) {
System.out.print(" ");
}
System.out.println("|");
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++) {
System.out.print(" ");
}
System.out.print("___");
}
}
}
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
static int gridSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
for(int x = 0; x < gridSize; x++){
printTree();
printStem();
System.out.println();
}
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
System.out.println("What grid size do you want?");
gridSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for(int x = 0; x < gridSize; x++){
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("\\");
int width = 2 * (sectionSize + numSections - 1) + 1;
if(x != gridSize - 1){
for(int y = sectionSize + numSections + k + i; y < width; y++)
System.out.print(" ");
System.out.print(" ");
}
}
System.out.println();
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++)
System.out.print(" ");
System.out.print("|");
if(x != gridSize - 1)
for (int j = 0; j <= sectionSize + numSections; j++)
System.out.print(" ");
}
System.out.println();
}
for (int i = 0; i < 1; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++)
System.out.print(" ");
System.out.print("___");
if(x != gridSize - 1)
for (int j = 0; j < sectionSize + numSections; j++)
System.out.print(" ");
}
}
}
}
width_of_double_pine_tree = 2 * width_of_single_pine_tree + buffer_between_trees
For loops don't have to define a variable, they can use an existing one.
int j = 0;
for(; j < (width_of_double_pine_tree - (width_of_single_pine_tree + buffer_between_trees)); j++)
//this is your first tree
for(; j < (width_of_double_pine_tree - width_of_single_pine_tree); j++)
//this is your buffer (S.o.p spaces)
for(; j < (width_of_double_pine_tree); j++)
//this is your second tree
Your canvas is always a rectangle, you subtract from it, the edge spaces you aren't using, void space with spaces (tabs ruin everything, because they aren't uniform, use spaces in everything!)
Use your i variable to keep track of the height that you're currently painting on your canvas so you can math in your pine needles and trunk. For the second tree, in the math portion, offset your j by the buffer_between_trees and width_of_single_tree if(i < (j + buffer + width)), etc.
Make sure you do the assignment before you tackle extra stuff.

Java out patterns

I'm currently trying to get this output using nested loops:
For the life of me I cannot figure out how to get the # signs to increment by +2 each time. Any help would be greatly appreciated!
public class PrintPatterns {
public static void main(String[] args) {
pattern1();
}
private static void pattern1() {
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < 10; j += 2); {
System.out.print("# ");
}
for (int j = 0; j < 2; j++) {
System.out.print(". ");
}
for (int j = 1; j < 10 - i; j++) {
System.out.print("x ");
}
System.out.println();
}
}
}
This code does what you want:
private static void pattern1() {
StringBuilder stringBuilder = new StringBuilder();
for (int ats = 2; ats <= 10; ats += 2) {
for (int j = 0; j < ats; j++) {
stringBuilder.append("# ");
}
stringBuilder.append(". . ");
for (int j = 0; j <= 10 - ats; j++) {
stringBuilder.append("x ");
}
stringBuilder.append("\n");
}
System.out.println(stringBuilder.toString());
}
One mistake you did in your code is that you put a ; after your for loop, this will end the loop right there.
Also do not use System.out.println() in loops. As using IO will slow down your application. Use StringBuilder to build strings and then output all at once.
Instead of:
for (int j = 0; j < 10; j += 2); {
System.out.print("# ");
}
Try:
for (int j = 0; j < 2 * i; j += 1) {
System.out.print("# ");
}
public class PrintPatterns
{
public static void main(String[] args)
{
pattern1();
}
private static void pattern1()
{
for(int i = 1; i <= 10; i++)
{
for(int j = 1; j < i+2; j++)
{
System.out.print("# ");
}
for(int j = 0; j < 2; j++)
{
System.out.print(". ");
}
for(int k = 10-i; k > 0; k--)
{
System.out.print("x ");
}
System.out.println();
}
}
}

About diamond generation code in Java

This is the work that i done so far, i'manage to do is just create a triangle,So how can i extend this to create a Diamond pattern
class Diamond {
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");}
}
You have to change the program like this, first you have to keep in mind
that you have to create empty spaces, then only you'll be able
to create this pattern,this is sample code.You have to create an
Upside down triangle, like you done so far,
class Diamond {
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}

Having Trouble using For Loops to make two triangles of different characters fit into a rectangle?

Examples of input:
3
4
Examples of output (assume that spaces = new lines.)
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
So far, the fragment of code that attempts to print this is (Assume that all variables are pre-defined):
public int getSize()
{
for (int i = size; i > 0; i--){
for (int j = 1; j < size; j++){
out.print("Q");
out.print("H");
}
out.println("");
}
return 0;
}
It just prints: (assume that spaces = new lines.)
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
For input of 5. I'm not quite sure how to make it print only the number of times of its respective integer value. Can someone explain?
You could break the inner loop it two, like this:
for (int i = size; i > 0; i--) {
for (int j = 0; j < i; j++) {
out.print("Q");
}
for (int j = i; j < size + 1; j++) {
out.print("H");
}
out.println();
}
Output:
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
Or if you don't want to break the loop, you can use the ternary operator:
for (int i = size; i > 0; i--) {
for (int j = 0; j < size + 1; j++) {
out.print(j < i ? 'Q' : 'H');
}
out.println();
}
Try this
for (int i = 0; i < size; i++) {
for (int j = 1; j <= size-i; j++) {
System.out.print("Q");
}
for (int k = 0; k <= i; k++) {
System.out.print("H");
}
System.out.println("");
}
try this code block instead:
int j=0;
for (int i = size; i > 0; i--)
{
j=0;
while(j < i)
{
out.print("Q");
j++;
}
j=i;
while(j < size+ 1)
{
out.print("H");
j++;
}
out.println();
}
Tested with sample inputs. Working fine
public int getSize() {
for (int i = 1; i < size+1; i++) {
for (int j = 0; j < size+1; j++) {
int Qtimes = size-i;
if(j <= Qtimes) {
System.out.print("Q");
} else{
System.out.print("H");
}
}
System.out.println("");
}
return 0;
}
This works if the input is 4 - for example -change it to any number
public int getSize()
{
int cnt = 0;
int i,j,k = 0;
for ( i = 4; i > 0; i--){
for ( j = 0; j < i; j++){
System.out.print("Q");
}
cnt ++;
for( k = 0 ; k <cnt ; k++) {
System.out.print("H");
}
System.out.println("");
}
return 0;
}
output is
QQQQH
QQQHH
QQHHH
QHHHH

Categories