Jump to content

Java Triangle

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Right, So a project I did a few days ago required the following of me :

Quote

6. Write a class named RightTriangle that extends Shape. Add two int
fields for the base and height of the triangle, and add a constructor
with two int parameters that are used to initialize these fields. Use
encapsulation to ensure that the value of base and height are
between 1 and 20.
7. In your RightTriangle class, override the draw() method in Shape.
Using nested for loops and asterisks, print out the triangle similarly
to the way you printed out the Rectangle. For example, if the base is
8 and the height is 4, the output should look similar to:
*
***
******
********

I wrote a code that, according to me atleast, should be working 100%. But it doesn't. I get the program to draw me a Triangle but it doesnt work out exactly as in the project request.

No use in me posting the entire class, so I'm just going to post the method that gets invoked that must "draw" the triangle.


	public void draw()

	{

		int chan = (int)this.base/this.height;		


		for( int a = 1; a < this.height; a++ )

		{

			double sum = Math.round((this.base/(this.base-chan)) * a);

			for( int b = 0; b < sum; b++ )

			{

				System.out.print("*");

			}

			System.out.print("\n");

		}

		

		for( int c = 0; c < this.base; c++ )

		{

			System.out.print("*");

		}

		

		System.out.print("\n");

	}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
There seems to be no logic in the triangle you were given 1,3,6,8 ? Without logic it's impossible to do.

#3
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Well, that was my first thoughts. However, mathematically , I was able to work it out as such. And since they give you the height aswell as the base, there must be a way to work it out correctly. Using the height I can controll the loop as to how many times it must execute and using the base I can work out how many asteriks must be added per line. But the problem is that the rounding isn't occuring correctly. 1.6 for instance rounds to 1 in my script instead of rounding of to 2

#4
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Hi, in case you didn't solve it yourself yet I've written a program that writes triangle the way they want it to although it might not forfill all other criteria for example I didn't extend shape and I don't quite see the relation between encapsulation and seeing to it that base and height are at least 20 but I'm leaving it up to you to write methods for checking etc. anhyway here are those two programs that should do the trick, if you don't get something please ask I'll gladly explain if I can, and if there is some bug please tell because I found myself being really annoyed by the program because "how hard can it be to make something write triangles with the stars" hehe)

Main class that I'va called triangle:
import java.util.Scanner;


public class Triangle 

{

	public static void main(String[] args)

	{

		Scanner kbd = new Scanner(System.in);

		System.out.println("Enter the base:");

		int base = kbd.nextInt();

		System.out.println("Enter the height");

		int height = kbd.nextInt();

		RightTriangle rTriangle = new RightTriangle(base, height);

		rTriangle.draw();

	}


}

and the rightTrangle class:

public class RightTriangle 

{

	private int base;

	private int height;

	

	public RightTriangle(int b, int h)

	{

		base = b;

		height = h;

	}

	

	public String toString()

	{

		return base + " " + height;

	}

	

	public int getBase()

	{

		return base;

	}

	

	public int getHeight()

	{

		return height;

	}

	

	public void draw()

	{

		int temp1, temp2;

		

		if (base > height)

		{

			System.out.println("*");

			temp1 = Math.round(base / (height - 1));

			temp2 = temp1;

			

			for(int i = 1; i < height; i++)

			{

				for(int j = 0; j <= temp1 && j < base; j++)

				{

					System.out.print("*");

				}

				temp1 += temp2;

				System.out.println();

			}

		}

		else if(height > base)

		{

			if(Math.round(height / base) % 2 != 0 && base != 2)

			{

				temp1 = Math.round(height / (base-1));	

				if(temp1 == 1)

					temp1 += 1;

			}

			else

			{

				temp1 = Math.round((height / base));

				if(temp1 == 1)

					temp1 += 1;

			}

			temp2 = 1;

			

			for(int i = 1; i <= height; i++)

			{

				for(int j = 1; j <= temp2 && j <= base; j++)

				{

					System.out.print("*");

				}

				if(i % temp1 == 0)

				{

					temp2 += 1;

				}

				System.out.println();

			}

			

		}

		

		else

		{

			for(int i = 1; i <= height; i++)

			{

				for(int j = 1; j <= i; j++)

				{

					System.out.print("*");

				}

				System.out.println();

			}

		}

	}


}


P.S since you were planning on extending the class from some other I've rewritten toString() as well.

#5
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts

Roman Y said:

Hi, in case you didn't solve it yourself yet I've written a program that writes triangle the way they want it to although it might not forfill all other criteria for example I didn't extend shape and I don't quite see the relation between encapsulation and seeing to it that base and height are at least 20 but I'm leaving it up to you to write methods for checking etc. anhyway here are those two programs that should do the trick, if you don't get something please ask I'll gladly explain if I can, and if there is some bug please tell because I found myself being really annoyed by the program because "how hard can it be to make something write triangles with the stars" hehe)

Main class that I'va called triangle:
import java.util.Scanner;

public class Triangle 
{
	public static void main(String[] args)
	{
		Scanner kbd = new Scanner(System.in);
		System.out.println("Enter the base:");
		int base = kbd.nextInt();
		System.out.println("Enter the height");
		int height = kbd.nextInt();
		RightTriangle rTriangle = new RightTriangle(base, height);
		rTriangle.draw();
	}

}

and the rightTrangle class:
public class RightTriangle 
{
	private int base;
	private int height;
	
	public RightTriangle(int b, int h)
	{
		base = b;
		height = h;
	}
	
	public String toString()
	{
		return base + " " + height;
	}
	
	public int getBase()
	{
		return base;
	}
	
	public int getHeight()
	{
		return height;
	}
	
	public void draw()
	{
		int temp1, temp2;
		
		if (base > height)
		{
			System.out.println("*");
			temp1 = Math.round(base / (height - 1));
			temp2 = temp1;
			
			for(int i = 1; i < height; i++)
			{
				for(int j = 0; j <= temp1 && j < base; j++)
				{
					System.out.print("*");
				}
				temp1 += temp2;
				System.out.println();
			}
		}
		else if(height > base)
		{
			if(Math.round(height / base) % 2 != 0 && base != 2)
			{
				temp1 = Math.round(height / (base-1));	
				if(temp1 == 1)
					temp1 += 1;
			}
			else
			{
				temp1 = Math.round((height / base));
				if(temp1 == 1)
					temp1 += 1;
			}
			temp2 = 1;
			
			for(int i = 1; i <= height; i++)
			{
				for(int j = 1; j <= temp2 && j <= base; j++)
				{
					System.out.print("*");
				}
				if(i % temp1 == 0)
				{
					temp2 += 1;
				}
				System.out.println();
			}
			
		}
		
		else
		{
			for(int i = 1; i <= height; i++)
			{
				for(int j = 1; j <= i; j++)
				{
					System.out.print("*");
				}
				System.out.println();
			}
		}
	}

}

P.S since you were planning on extending the class from some other I've rewritten toString() as well.

Woah, It actually works! Thanks allot, I've been bugged with this stupid thing since last week. Its not a homework assignment or anything but heck it sure did get to me, as you said its just a freaking triangle. Not suppose to be this hard :P

Thanks allot for the time and effort you spent on this mate. :)

#6
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
No problem always happy to help + it was a good exercise for me as well)))