Printing the Letters O and P using for loop(toString())

Printing O and P

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1463 Views - Last Post: 19 August 2009 - 06:03 AM Rate Topic: ****- 1 Votes

#1 boredpig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-August 09

Printing the Letters O and P using for loop(toString())

Post icon  Posted 18 August 2009 - 02:12 AM

/**
* Prints Letter L
*
* @author
* @version 18 August 2009
*/
public class LetterL
{
private int height;
private int width;

public LetterL(int initHeight, int initWidth)
{
height = initHeight;
width = initWidth;
}

public void print()
{
System.out.println(toString());
}

public String toString()
{
String letter = "";
for (int i=2;i<=width;i++)
{
letter += "*\n";
if(i==height)
{
for(int j = 0; j<width; j++)
{
letter += "*";
}

}
}
return letter;
}
}
// I used BlueJ for this, can anyone alter this code so that it would print O and P? (separate classes with LoopTester)
The attached file shows the expected output...

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Printing the Letters O and P using for loop(toString())

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2795
  • View blog
  • Posts: 15,910
  • Joined: 24-September 08

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 05:45 AM

:code: Thanks.

I'm not quite understanding your problem. Can you explain it a little more? Sorry. I'm a little tired to be decifering broad statements :P
Was This Post Helpful? 0
  • +
  • -

#3 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 06:24 AM

he wants us to edit the code for him so that the output would look like the attached file.

let's start by reminding you to use the [ code] and [/ code] tags :)

next, we can't do the work for you. you are asking us to alter the code, we can't do that, but we can help you fix it the way you want it.

does it have to be separate classes?
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2795
  • View blog
  • Posts: 15,910
  • Joined: 24-September 08

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:01 AM

Well, I can't download attachements on a DoD computer, so I needed a better explanation. Sorry.
Was This Post Helpful? 0
  • +
  • -

#5 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:13 AM

it says LOOP in "*"'s vertically...
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2795
  • View blog
  • Posts: 15,910
  • Joined: 24-September 08

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:31 AM

You mean like

*
*
*
*
*****

  ***
*	  *
*	  *
*	  *
  ***

  ***
*	  *
*	  *
*	  *
  ***

***
*	 *
***
*
*



???

This post has been edited by NeoTifa: 18 August 2009 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:33 AM

those look more like D's, but pretty much...

[edit] i looked again and they're O's. sorry neo

i would say only have different methods for the letters then have a master method that prints it :)

This post has been edited by computerfox: 18 August 2009 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2795
  • View blog
  • Posts: 15,910
  • Joined: 24-September 08

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:39 AM

Yea, I fixed them.

Think about what they're doing. The first and last row are, in my case 2 spaces, 3 *, and 2 spaces again. The rest are *, 6 spaces, and another *. Depending on the height and width, these change. Plus, in your compiler it will output differently. I had to do that due to the way the forums prints stuff.
Was This Post Helpful? 0
  • +
  • -

#9 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:41 AM

is there some way to take out the space at the end of the L?

wait there is.... hold on

i wasn't going to write the code, but i think i must now...
Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2795
  • View blog
  • Posts: 15,910
  • Joined: 24-September 08

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:51 AM

Don't post it.
Was This Post Helpful? 0
  • +
  • -

#11 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:58 AM

i know, i know...


just finished my version. it's really not that bad if you use methods, even if you are required to use loops. it actually makes it easier with loops. just look at the final product and tell me what you think must be done for each of those letters separately then combine it with another method.

This post has been edited by computerfox: 18 August 2009 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 04:24 PM

The easiest way would be tho have the alphabet as a serie of String


String[][] alphabet = {
{"	 A ",
  "   A A",
  "   AAA",
  "  A   A"
  "  A   A"},
{" BBB",
  "B	 B",
  "BBBB",
  "B	 B",
  "BBBB"},
{"CCCCC",
  "C"
  "CCC",
  "C",
  "CCCCC"},
.....


then you can pass a "loop" to a method that decompose the word received as parameter and loop to print alphabet[letterValue][]...
Was This Post Helpful? 0
  • +
  • -

#13 malerv  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 100
  • Joined: 01-July 09

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:03 PM

With you're way to do, how do you scale the letter pbl?
Was This Post Helpful? 0
  • +
  • -

#14 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Printing the Letters O and P using for loop(toString())

Posted 18 August 2009 - 07:32 PM

mmmm i think i found the solution of your probleme although it has taken time to acheive it but it worth it
see this code

package test;

public class LetterL
{
private int height;
private int width;

public LetterL(int initHeight, int initWidth)
{
height = initHeight;
width = initWidth;
}

public void print()
{
System.out.println(toStringL());
System.out.println(toStringO());
System.out.println(toStringO());
System.out.println(toStringP());
}

public String toStringL()
{
String letter = "";
for (int i=0;i<=height;i++)
{
letter += "*\n";
if(i==height)
{
for(int j = 0; j<width; j++)
{
letter += "*";
}

}
}
return letter;
}
public String toStringP()
{
String letter = "";
for (int i=0;i<=height;i++)
{

	if(i==0)
	{
	for(int j = 0; j<width; j++)
	{
	letter += "*";
	}
	letter += "\n";
	}
	if(i<height%3)
	{
		for(int j = 0; j<width; j++)
		{
			if(j==width-1||j==0)
				letter += "*";
			else
			letter += " ";
			
		}
		letter += "\n";
	}
	else	if(i==(height%3))
	{
		letter += "*";
		for(int j = 0; j<width-1; j++)
		{
			if(j==width-2)
				letter += "*\n";
			else
			letter += "*";
			
		}


	}
	else
	
letter += "*\n";


}
return letter;
}
public String toStringO()
{
	String letter = "";
	for (int i=0;i<=height-1;i++)
	{if(i==0)
	{
		for(int j = 0; j<width; j++)
		{
		letter += "*";
		}
		letter+="\n";
	}
	
	if(i!=0 || i!=height-1){
		for(int j = 0; j<width; j++)
		{if(j==0)
			letter += "*";
		else
		letter += " ";
		if(j==width-2)
		{
			letter+="*\n";
			j++;
		}
		}
	}
	
	if(i==height-1)
	{
	for(int j = 0; j<width; j++)
	{
	letter += "*";
	}

	}
	}
	return letter;

}
public static void main(String[] args)
{
	LetterL l=new LetterL(8,6);
	l.toStringL();
	l.toStringO();
	l.print();
	
}
}



with this methode here in the main function
public static void main(String[] args)
{
	LetterL l=new LetterL(8,6);

	
}

you will be able to determine the width and height that is suitable for you for example:

this width and height LetterL l=new LetterL(8,6);
will give this output:
(dont forget to name the extension of the downloaded file as .bmp)
loop1 in the atachment

and this input LetterL l=new LetterL(3,4);
will give you this output wich will be smaller:
(dont forget to name the extension of the downloaded file as .bmp)
loop2 in the attachment
Hope its useful :)

Attached File(s)

  • Attached File  loop1.bmp (68.3K)
    Number of downloads: 57
  • Attached File  loop2.bmp (141.22K)
    Number of downloads: 51

This post has been edited by oQMr FoxQo: 18 August 2009 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#15 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Printing the Letters O and P using for loop(toString())

Posted 19 August 2009 - 05:52 AM

okay here's my code. very easy to manipulate and it's pretty much what you want.


import java.util.*;

public class loop {

	public static void l(){
		System.out.println();
		for(int i=0;i<6;i++){
			System.out.println("*");
		}
		for(int i=0;i<5;i++){
			System.out.print("* ");
		}
	}
	
	public static void o(){
		System.out.println("\n   ***   ");
		for(int i=0;i<4;i++)
			System.out.println("*		*");
		System.out.println("   ***	");
	}
	
	public static void p(){
		System.out.println();
		System.out.println(" ***\n");
		System.out.println(" *	  *\n");
		System.out.println(" ***");
		for(int i=0;i<4;i++)
			System.out.println(" *");
	}
		
	public static void drawLoop(){
		l();
		o();
		o();
		p();
	}
	public static void main (String args[]) {
		
		drawLoop();
	}
}




hope that was helpful :)

PS if you're gonna copy and past any code from us, don't just do that, i hope you try to actually learn what we did.

This post has been edited by computerfox: 19 August 2009 - 05:53 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2