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++;
}
}
12 Replies - 2697 Views - Last Post: 06 December 2011 - 04:14 PM
#1
need help to design this program using object oriented approach
Posted 05 December 2011 - 09:48 PM
Replies To: need help to design this program using object oriented approach
#2
Re: need help to design this program using object oriented approach
Posted 05 December 2011 - 10:00 PM
Quote
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
#3
Re: need help to design this program using object oriented approach
Posted 05 December 2011 - 10:11 PM
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();
}
}
#4
Re: need help to design this program using object oriented approach
Posted 05 December 2011 - 10:15 PM
smohd, on 05 December 2011 - 10:00 PM, said:
Quote
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.
#5
Re: need help to design this program using object oriented approach
Posted 05 December 2011 - 10:29 PM
pbl, on 05 December 2011 - 10:11 PM, said:
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.
#6
Re: need help to design this program using object oriented approach
Posted 05 December 2011 - 10:43 PM
#7
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)
#8
Re: need help to design this program using object oriented approach
Posted 05 December 2011 - 11:00 PM
pbl, 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.
#9
Re: need help to design this program using object oriented approach
Posted 05 December 2011 - 11:05 PM
#10
Re: need help to design this program using object oriented approach
Posted 06 December 2011 - 12:45 AM
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
#11
Re: need help to design this program using object oriented approach
Posted 06 December 2011 - 01:26 AM
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
#12
Re: need help to design this program using object oriented approach
Posted 06 December 2011 - 01:32 AM
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);
}
}
#13
Re: need help to design this program using object oriented approach
Posted 06 December 2011 - 04:14 PM
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...
|
|

New Topic/Question
Reply



MultiQuote




|