12 Replies - 2962 Views - Last Post: 06 December 2011 - 04:14 PM Rate Topic: -----

#1 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

need help to design this program using object oriented approach

Posted 05 December 2011 - 09:48 PM

Hi i m trying to design a program using object oriented approach.everything works perfect for me except the bounces.If the new height is less then 0 then multiply both the height and velocity by -0.5 to simulate the bounce.
import java.util.Scanner;
public class Ball11
{
	public static void main (String [ ] args)
	{
		double height = 0;

		int times = 0;

		int speed=0;
		int bounces=0;


		int numOfBounces = 0;
		Scanner scan= new Scanner (System.in);
		System.out.println("Enter the initial velocity of the ball:");
		height=scan.nextInt();

		System.out.println("Enter the no of speed");
		speed=scan.nextInt();

		System.out.println("enter the no of bounces");
		bounces=scan.nextInt();

		System.out.println();
//Allow the user to play the ball as many  times as he wants and each time he can decide the initial ball speed and the number of bounces to stop the ball



			do
			{
				times++;
				height =height + speed;

				speed-=32;

				if (height > 0)

					System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed);
				else
				{
					height = (double) (height * (-0.5));
					speed = (int) (speed * (-0.5));
					System.out.println ("bounce!");

					System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed );

					break;
				}
			} while (true);
			bounces++;




	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: need help to design this program using object oriented approach

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 10:00 PM

*Moved to Java* Please dont ask help questions in Java Programmer forum.

Quote

If the new height is less then 0 then multiply both the height and velocity by -0.5 to simulate the bounce.

Two things here:
- You are doing this if height is less than or equal o 0 in your code.
- You put a break, so it will be the last one, i this what you mean?
By the way, can you elaborate what is your problem

This post has been edited by smohd: 05 December 2011 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 10:11 PM

Quick and dirty, something like that ?

import java.util.Scanner;
public class Ball11
{
	double height;
	int speed, bounces;
	
	Ball11(double height, int speed, int bounces) {
		this.height = height;
		this.speed = speed;
		this.bounces = bounces;
	}
	
	void rebound() {
		int times = 0;
		do
		{
			times++;
			height =height + speed;

			speed-=32;

			if (height > 0)

				System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed);
			else
			{
				height = (double) (height * (-0.5));
				speed = (int) (speed * (-0.5));
				System.out.println ("bounce!");

				System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed );

				break;
			}
		} while (true);
		bounces++;

		
	}
	public static void main (String [ ] args)
	{
		Scanner scan= new Scanner (System.in);
		System.out.println("Enter the initial velocity of the ball:");
		double height=scan.nextDouble();

		System.out.println("Enter the no of speed");
		int speed=scan.nextInt();

		System.out.println("enter the no of bounces");
		int bounces=scan.nextInt();
        Ball11 b = new Ball11(height, speed, bounces);
        b.rebound();

	}
}


Was This Post Helpful? 0
  • +
  • -

#4 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 10:15 PM

View Postsmohd, on 05 December 2011 - 10:00 PM, said:

*Moved to Java* Please dont ask help questions in Java Programmer forum.

Quote

If the new height is less then 0 then multiply both the height and velocity by -0.5 to simulate the bounce.

Two things here:
- You are doing this if height is less than or equal o 0 in your code.
- You put a break, so it will be the last one, i this what you mean?
By the way, can you elaborate what is your problem


forexample if the user put 8 for bounces.so whenever the height will be less then zero my program will say bounce.and after the 8 bounce my program will stop.
Was This Post Helpful? 0
  • +
  • -

#5 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 10:29 PM

View Postpbl, on 05 December 2011 - 10:11 PM, said:

Quick and dirty, something like that ?

import java.util.Scanner;
public class Ball11
{
	double height;
	int speed, bounces;
	
	Ball11(double height, int speed, int bounces) {
		this.height = height;
		this.speed = speed;
		this.bounces = bounces;
	}
	
	void rebound() {
		int times = 0;
		do
		{
			times++;
			height =height + speed;

			speed-=32;

			if (height > 0)

				System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed);
			else
			{
				height = (double) (height * (-0.5));
				speed = (int) (speed * (-0.5));
				System.out.println ("bounce!");

				System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed );

				break;
			}
		} while (true);
		bounces++;

		
	}
	public static void main (String [ ] args)
	{
		Scanner scan= new Scanner (System.in);
		System.out.println("Enter the initial velocity of the ball:");
		double height=scan.nextDouble();

		System.out.println("Enter the no of speed");
		int speed=scan.nextInt();

		System.out.println("enter the no of bounces");
		int bounces=scan.nextInt();
        Ball11 b = new Ball11(height, speed, bounces);
        b.rebound();

	}
}


eg if i put 7 for no of bounces why i don't see 7 bounces in my output.
Was This Post Helpful? 0
  • +
  • -

#6 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 10:43 PM

eg if i put 7 for no of bounces why i don't see 7 bounces in my output.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 10:48 PM


        } while (true);
        bounces++;


don't know what you try to do but bouces is updated only once AFTER the loop while(true)
Was This Post Helpful? 0
  • +
  • -

#8 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 11:00 PM

View Postpbl, on 05 December 2011 - 10:48 PM, said:


        } while (true);
        bounces++;


don't know what you try to do but bouces is updated only once AFTER the loop while(true)

i want my program to print the word bounce.suppose for "enter the no bounces" if i put 5.then everytime the new height is less then zero my program will print "bounce" .after 5th bounce my progrm will stop.

i want my program to print the word "bounce".suppose for "enter the no bounces" if i put 5.then everytime the new height is less then zero my program will print "bounce" .after 5th bounce my progrm will stop.
Was This Post Helpful? 0
  • +
  • -

#9 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

Re: need help to design this program using object oriented approach

Posted 05 December 2011 - 11:05 PM

i want my program to print the word "bounce".suppose for "enter the no bounces" if i put 5.then everytime the new height is less then zero my program will print "bounce" .after 5th bounce my progrm will stop.
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: need help to design this program using object oriented approach

Posted 06 December 2011 - 12:45 AM

That is because your loop does not depend on number of bounces, but the height and speed. So it will loop until height is less than 0 and stop. The bounce does not change in your loop.
The loop only shows bounce once because of the break; statement, you exit the loop.
What you can do is removing the break statement and making a count that counts how many number the counts occurs, and your loop condition will be:
} while (count < bounces);
or like so
Was This Post Helpful? 1
  • +
  • -

#11 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

Re: need help to design this program using object oriented approach

Posted 06 December 2011 - 01:26 AM

ok i remove break and i put (count<bounces)but it still have no affect.and my program does not stop and keep running.
import java.util.Scanner;
public class Ball111
{
	public static void main (String [ ] args)
	{
		double height = 0;

		int times = 0;

		int speed=0;
		int bounces=0;
		int count=0;





		Scanner scan= new Scanner (System.in);
		System.out.println("Enter the initial velocity of the ball:");
		height=scan.nextInt();
		System.out.println();

		System.out.println("Enter the no of speed");
		speed=scan.nextInt();
		System.out.println();

		System.out.println("enter the no of bounces");
		bounces=scan.nextInt();

		System.out.println();
//Allow the user to play the ball as many  times as he wants and each time he can decide the initial ball speed and the number of bounces to stop the ball

System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed);

	do
	{
			do
			{
				count++;
				times++;
				height =height + speed;

				speed-=32;

				if (height > 0)

					System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed);
				else
				{
					height = (double) (height * (-0.5));
					speed = (int) (speed * (-0.5));
					System.out.println ("bounce!");

					System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed );




					bounces++;

				}
			} while (true);
		} while (count<bounces);






	}
}

This post has been edited by smohd: 06 December 2011 - 03:56 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#12 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

Re: need help to design this program using object oriented approach

Posted 06 December 2011 - 01:32 AM

ok i remove break and i put (count<bounces)but it still have no affect.and my program does not stop and keep running.
import java.util.Scanner;
 public class Ball111
 {
 public static void main (String [ ] args)
 {
 double height = 0;
 
int times = 0;
 
int speed=0;
 int bounces=0;
 int count=0;
 




Scanner scan= new Scanner (System.in);
 System.out.println("Enter the initial velocity of the ball:");
 height=scan.nextInt();
 System.out.println();
 
System.out.println("Enter the no of speed");
 speed=scan.nextInt();
 System.out.println();
 
System.out.println("enter the no of bounces");
 bounces=scan.nextInt();
 
System.out.println();
 //Allow the user to play the ball as many times as he wants and each time he can decide the initial ball speed and the number of bounces to stop the ball
 
System.out.println ("time = " + times + " height = " + height + " speed = " + speed);
 
do
 {
 do
 {
 count++;
 times++;
 height =height + speed;
 
speed-=32;
 
if (height > 0)
 
System.out.println ("time = " + times + " height = " + height + " speed = " + speed);
 else
 {
 height = (double) (height * (-0.5));
 speed = (int) (speed * (-0.5));
 System.out.println ("bounce!");
 
System.out.println ("time = " + times + " height = " + height + " speed = " + speed );
 



bounces++;
 
}
 } while (true);
 } while (count<bounces);
 





}
 } 


Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: need help to design this program using object oriented approach

Posted 06 December 2011 - 04:14 PM

You have two loops now, the inner loops loops to infinity. Remove the inner loop you have no need of that.
So your program will have one loop that loop according to the count up to bounce.
Also the one to increment is not bounce, it is count. So change bounces++; to count ++;. Because it is the one counting...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1