473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Creating a Pattern Assignment

Hey,

I'm having trouble with an assignment and I need some help.

basically, the assignment is the same as the from the following link:
http://www.thescripts.com/forum/thread607498.html

However, there is one major exception: The pattern is reversed. Here is what it looks like:
If the user types "3", your program should produce the following output:
***
**
*
If the user types "9", your program should produce:
*********
********
*******
******
*****
****
***
**
*

I found out how to do the other pattern of my own but I am having trouble figuring out how to flip it.

Here is my code so far:
------------------------------------------------------------------
import java.io.*;

public class MagicPrinter
{
public static void main(String[] args)
throws java.io.IOException
{
String s1, s2, s3, total;
int num, minnum;
char a = 'a';
String star = "*";

total = "";

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("How many lines? ");
s1 = br.readLine();
num = Integer.parseInt(s1);

minnum = 1;

do
{
total = total + star;
minnum++;

System.out.println(total);
}
while(minnum <= num);
}
}
----------------------------------------------------------------

Any help would be apprecitated. Thanks.
Mar 9 '08 #1
7 2100
sukatoa
539 512MB
Hey,

I'm having trouble with an assignment and I need some help.

basically, the assignment is the same as the from the following link:
http://www.thescripts.com/forum/thread607498.html

However, there is one major exception: The pattern is reversed. Here is what it looks like:
If the user types "3", your program should produce the following output:
***
**
*
If the user types "9", your program should produce:
*********
********
*******
******
*****
****
***
**
*

I found out how to do the other pattern of my own but I am having trouble figuring out how to flip it.

Here is my code so far:
------------------------------------------------------------------
import java.io.*;

public class MagicPrinter
{
public static void main(String[] args)
throws java.io.IOException
{
String s1, s2, s3, total;
int num, minnum;
char a = 'a';
String star = "*";

total = "";

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("How many lines? ");
s1 = br.readLine();
num = Integer.parseInt(s1);

minnum = 1;

do
{
total = total + star;
minnum++;

System.out.println(total);
}
while(minnum <= num);
}
}
----------------------------------------------------------------

Any help would be apprecitated. Thanks.
Algorithm:

Assuming my input is 7

Expand|Select|Wrap|Line Numbers
  1. while( input not equal to 0 )
  2.  
  3.        Print the ' * ' input times...
  4.        Create new line...
  5.        Decrement the input...

Sukatoa (Shadow Shaman)
Mar 9 '08 #2
Algorithm:

Assuming my input is 7

Expand|Select|Wrap|Line Numbers
  1. while( input not equal to 0 )
  2.  
  3.        Print the ' * ' input times...
  4.        Create new line...
  5.        Decrement the input...

Sukatoa (Shadow Shaman)
I tried this and it is giving me the output

*
*
*
*
*
*
*
*
*
This is what I typed:
while (num != 0)
{
System.out.println(star);
num--;
}

when I also try:
while (num !=0)
{
total = total + star;
System.out.println(total);
num--;
}

I get the same pattern I had to begin with. Maybe I'm just unsure of what you meant by print '*' input times...
Mar 9 '08 #3
sukatoa
539 512MB
I tried this and it is giving me the output

*
*
*
*
*
*
*
*
*
This is what I typed:
while (num != 0)
{
System.out.println(star);
num--;
}

when I also try:
while (num !=0)
{
total = total + star;
System.out.println(total);
num--;
}

I get the same pattern I had to begin with. Maybe I'm just unsure of what you meant by print '*' input times...
print '*' input times is = Print the asterisk many times depend on the input, if my input is currently 7, then print the asterisk 7 times in the same line...

Maybe you forgot this,

What is the difference between

Expand|Select|Wrap|Line Numbers
  1. System.out.print("Something");
and

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Something");
If you can differentiate that, then proceed... If you are not sure, make some experiments about the two...


Update us.
Sukatoa
Mar 9 '08 #4
print '*' input times is = Print the asterisk many times depend on the input, if my input is currently 7, then print the asterisk 7 times in the same line...

Maybe you forgot this,

What is the difference between

Expand|Select|Wrap|Line Numbers
  1. System.out.print("Something");
and

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Something");
If you can differentiate that, then proceed... If you are not sure, make some experiments about the two...


Update us.
Sukatoa
I know the difference between the methods; println returns a new line after completing while print prints subsequent information on the same line. I played around with many different things and this is what I ended with

while (num != 0)
{
total = total + star;
System.out.print(total);
System.out.print("");
num--;
}

When imputing 9 I get the required number of stars but on the same line. When i use println for the second method I just get the same result I had to begin with.
Mar 9 '08 #5
sukatoa
539 512MB
You must understand the code below before you attempt to try it, if you don't follow my instructions, later you will be dependent from the others....

I hope you will understand this....

Expand|Select|Wrap|Line Numbers
  1. public class demo{
  2.     public static void main(String sukatoa[]){
  3.         int input = new java.util.Scanner(System.in).nextInt();
  4.         while(input > 0){
  5.             System.out.println();
  6.             for(int x=0;x<input;x++){
  7.                 System.out.print("*");
  8.             }input--;
  9.         }System.out.println();
  10.     }
  11. }
The code i've posted above will be deleted after a few minutes by the moderator... Please, read the book carefully next time...

Try to make have some experiments... Later you will come-up an idea...

Worrying
Sukatoa.
Mar 9 '08 #6
You must understand the code below before you attempt to try it, if you don't follow my instructions, later you will be dependent from the others....

I hope you will understand this....

Expand|Select|Wrap|Line Numbers
  1. public class demo{
  2.     public static void main(String sukatoa[]){
  3.         int input = new java.util.Scanner(System.in).nextInt();
  4.         while(input > 0){
  5.             System.out.println();
  6.             for(int x=0;x<input;x++){
  7.                 System.out.print("*");
  8.             }input--;
  9.         }System.out.println();
  10.     }
  11. }
The code i've posted above will be deleted after a few minutes by the moderator... Please, read the book carefully next time...

Try to make have some experiments... Later you will come-up an idea...

Worrying
Sukatoa.
Thank You. I now see. So first comes the space and then the input, along with the corresponding amount of stars. The input is then decreased by one and a new line is then printed whereby the decreased amount of stars follows the same process until it reaches 1. At least I think I've now got it.
Mar 9 '08 #7
sukatoa
539 512MB
Thank You. I now see. So first comes the space and then the input, along with the corresponding amount of stars. The input is then decreased by one and a new line is then printed whereby the decreased amount of stars follows the same process until it reaches 1. At least I think I've now got it.
Nice flow...

No probz.. ;-)

Sukatoa...
Mar 9 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Brian | last post by:
Hi all... This question is more for the GURUs out there. It is not a question on how to do something, but why it happens, and I am trying to figure out if there is a pattern. I am using IE, but...
2
by: qazmlp | last post by:
Builder pattern seem to suit my requirements perfectly except in one case as described below. I have used the same example diagram that is used in GOF Design pattern book, to explain the problem...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
6
by: Ian Pilcher | last post by:
The more I think about pointers, the more my head hurts. From what I can tell, nothing in the standard guarantees that pointer assignment preserves bit patterns. Section 6.5.16.1, paragraph 2,...
1
by: Marcel Hug | last post by:
Hi NG ! I have already written a task about MVC and I tried to get the best informations together. I would like to implement the MVC pattern and it work on the way I did it. At first i know the...
5
by: Eric | last post by:
I am implementing a variation on the Singleton design pattern, that allows up to 8 objects of a class to be instantiated, and returns a null pointer for anything more than 8. I am running into a...
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.