Thank you so much in advance! I really need some help. The following variables w
ID: 3764250 • Letter: T
Question
Thank you so much in advance! I really need some help.
The following variables will be used in the code:
Type
Radius
Diameter
meltMofidier (0.05)
Random Number Generator (static)
snowFall (static)
The following methods are required:
getType()
getDiameter()
getRadius()
toString()
The following method is abstract:
melt();
Instructions:
Initialize the variables in the super class
The diameter should be of a random size of type double multiplied by a factor of random numbers from 8 to 10.
The radius is half the size of the diameter
The type is the one defined in the variable
The melt method should reduce the size of the diameter by a factor of (the type plus the meltModifier). This can be accomplished by dividing the diameter by (the type plus the meltModifier)
I need to crate a recursive function called: createSnowBall.
1. createSnowBall
1. This method has three parameters 1. desiredSize 2. currentSize 3. ArrayList snowBall
2. This method will recurisively call the create snowball method untill the desired size is reached.
3. If the current size is not greater than the desired size 1. we will add a snowflake to the snowball using the createSnowFlake method 2. increase the current size to account for the new snowflake.
This is the code that was already provided.
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class SnowBallFactory {
static Random gen = new Random(System.currentTimeMillis());
public static void main(String[] args) {
ArrayList<SnowFlake> snowBall = new ArrayList<SnowFlake>();
Scanner input = new Scanner(System.in);
System.out.println("Snow Ball Factory up and running.");
int balls = 3;
for(int j = 0; j < balls; j++){
System.out.print(" Enter desired size of snowball to be created:");
double desiredSize = input.nextDouble();
System.out.println("Created snow ball of size : " + desiredSize);
createSnowBall(desiredSize, 0, snowBall);
// Snow ball analysis
System.out.println("There are " + snowBall.size() + " snowFlakes.");
double size = 0.0;
for (SnowFlake s : snowBall) {
size += s.getDiameter();
}
System.out.println("Actual Size " + size);
// How long to melt?
int minutes = 0;
while (!snowBall.isEmpty()) {
for (SnowFlake s : snowBall) {
s.melt();
}
for (int i = snowBall.size() - 1; i >= 0; i--) {
if (snowBall.get(i).getDiameter() < 1.0) {
snowBall.remove(i);
}
}
minutes++;
}
System.out.println("It took " + minutes + " minutes to melt the snowball.");
}
System.out.println("SnowFall : " + SnowFlake.snowFall);
input.close();
}
public static void createSnowBall(double desiredSize, double currentSize, ArrayList<SnowFlake> snowball){
//Add code here to recursively call createSnowBall.
}
private static SnowFlake createSnowFlake() {
//Add code to match the types which you have chosen to implement.
int type = gen.nextInt(2) +1;
switch(type){
case(1):return new SimpleStar();
case(2):return new StellarDentrites();
}
return null;
}
}
Explanation / Answer
package snowballfactory;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class SnowBallFactory {
static Random gen = new Random(System.currentTimeMillis());
public static void main(String[] args) {
ArrayList<SnowFlake> snowBall = new ArrayList<SnowFlake>();
Scanner input = new Scanner(System.in);
System.out.println("Snow Ball Factory up and running.");
int balls = 3;
for(int j = 0; j < balls; j++){
System.out.print(" Enter desired size of snowball to be created:");
double desiredSize = input.nextDouble();
System.out.println("Created snow ball of size : " + desiredSize);
createSnowBall(desiredSize, 0, snowBall);
// Snow ball analysis
System.out.println("There are " + snowBall.size() + " snowFlakes.");
double size = 0.0;
for (SnowFlake s : snowBall) {
size += s.getDiameter();
}
System.out.println("Actual Size " + size);
// How long to melt?
int minutes = 0;
while (!snowBall.isEmpty()) {
for (SnowFlake s : snowBall) {
s.melt();
}
for (int i = snowBall.size() - 1; i >= 0; i--) {
if (snowBall.get(i).getDiameter() < 1.0) {
snowBall.remove(i);
}
}
minutes++;
}
System.out.println("It took " + minutes + " minutes to melt the snowball.");
}
System.out.println("SnowFall : " + SnowFlake.snowFall);
input.close();
}
public static void createSnowBall(double desiredSize, double currentSize, ArrayList<SnowFlake> snowball){
//Add code here to recursively call createSnowBall.
if(currentSize<desiredSize)
{
currentSize++;
createSnowBall(desiredSize,currentSize,snowball);
}
}
private static SnowFlake createSnowFlake() {
//Add code to match the types which you have chosen to implement.
int type = gen.nextInt(2) +1;
switch(type){
//case(1):return new SimpleStar();
//case(2):return new StellarDentrites();
}
return null;
}
}
//=================================================
import java.util.Random;
public class SnowFlake {
int Type;
double Radius;
double Diameter;
static Random gen;
static int snowFall;
public SnowFlake()
{
gen= new Random(System.currentTimeMillis());
}
public int getType()
{
return Type;
}
public double getDiameter()
{
return Diameter;
}
public double getRadius()
{
return Radius;
}
public String toString()
{
return "Type: "+Type+"and Radius: "+Radius;
}
void melt()
{
double meltMofidier=0.05;
Diameter/=Type+meltMofidier;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.