Hey everyone, I'm writing a program as follows:

CourseMarker course Java u4 exercise sunflower: Sunflower Growth part 2 (Weight: 3)
===================================================================================


Introduction
============

This is a continuation of last weeks exercise on the growth of sunflowers, but

the problem is now more complex. It now involves loops and makes sure that you

can keep track of your variables.


Problem
=======

Last week you had to read in details of sunflowers that grow at different
speeds. This week we shall be dealing with more than one sunflower.

Therefore you are to write a program to repeatedly read
a rate of growth in cm per day (double)
the number of days growth (int)
and compute and print the height of the sunflower at the end of the time in both
centimetres, and metres and centimetres (see typical output)
This week you will also have to keep track of the number of sunflowers planted,

and also print out the average height of the sunflowers at the end.

Also, our friendly sunflower gardener has realised there is money to be made
from his venture. He is able to sell his sunflowers but only when they are
at least 120cm tall, but less than 160cm. So he would be very grateful if you
could tell him on which days he is able to sell his plants. This will
involve you printing either

The Sunflower can be sold between days 9 and 10

or

This Sunflower cannot be sold

depending on whether the sunflower is between those heights on anyday.

Because we are repeatedly reading in values, you are to ask the user if they
want to enter details for another sunflower.

Plant another Sunflower (y/n):

You are to keep reading in values for the sunflowers until the user enters an 'N'
or 'n' when you are to print the total number of sunflowers printed.

The error conditions are the same as last week.


Method
======

Read the growth rate as a double and the number of days as an int. Then compute the
height of the sunflower:
height = growthRate * numDays
However, because we need to know when/if the height passes certain limits, it
might well be easier for you to increase the height value a day at a time
and then perform checks each day.

You will also need a variable to keep track of how many sunflowers have been
planted, obviously you should not include ones who have invalid growth
rates/number of days. Again you will need a variable to help you work out
the average height of the sunflowers.

Print the results as a whole number of centimetres (rounding down) and then in
both metres and centimetres.

If the user enters a growth rate of less than or equal to 0 cm per day you are to
print the message and exit the program, do not ask them to enter a number of
days:

Error, invalid growth rate!

If the number of days growth entered is less than 1 or greater than 50 (as that's when
the display needs to be ready) you should print the following error message and exit
the program:

Error with the number of days!

Once the results have been printed you are to ask the user if they want to plant
another sunflower, if so then you are to loop round continually until the user
says no.

Once the user has finished entering sunflowers you are to print the number planted.


You have planted 1 sunflower.

You are also to print the average sunflower height.

The average height is 123cm


Unless of course more than 1 has been planted when you should replace sunflower by sunflowers in the plural. Do not count sunflowers that have invalid data.


Hints
=====

The question may seem very long and complicated, but really only consists of a

couple of parts. Try to break it down into what you think you need to do.
e.g.
read values
make it grow
check size
print

The question is also based on last weeks exercise, so you may be able to get
inspiration from there.


Notes
=====

You have five (5) submissions for this exercise.
Make sure you only count sunflowers where you print out the final height.
When you ask the user if they want to enter another sunflower, you should
only accept Y or y as a positive result anything else is negative and should be

dealt with as a no.
Constants should be static and final.


Error Conditions
================

If the user enters a growth rate of less than or equal to 0 cm per day you are to
print the message and exit, do not ask them to enter a number of
days:

Error, invalid growth rate!

If the number of days growth entered is less than 1 or greater than 50 (as that's when
the display needs to be ready) you should print the following error message and exit:

Error with the number of days!


Mark Scheme
===========
20% : Typographical tests
50% : Dynamic tests
30% : Feature tests


Typical Input
=============

Enter the growth rate: 10
Enter the number of days growth: 17
Plant another Sunflower (y/n): n


Typical Output
==============

After 17 days growth at 10.0 cm a day it is now 170 cm tall
This converts to 1 metres and 70 centimetres
The Sunflower can be sold between days 12 and 15
You have planted 1 Sunflower.
The average height is 170 cm

I've attempted to program it but I've probably got it entirely wrong, I'm a beginner in Java but here is my attempt so far, with multiple errors it won't even compile :(

class Sunloops {
	
	// place global declarations here
	static final int maxDays = 50;
	static final int minDays = 1;
	static final int minGrowth = 0;
	char plantAnother;
	
	public static void main (String[] args) {
		
		// place local variables here
		double growthRate;
		int numDays;
		int height;
		double metre;
		double cm;
		int avgHeight;
		int numSun;
		int counter;
		double totalHeight;
		
		// prompt and read
		UserInput.prompt ("Enter the growth rate:");
		growthRate = UserInput.readDouble();
		if (growthRate <= 0) {
			System.out.println ("Error, invalid growth rate!");
			System.exit(-1);
		}		
		UserInput.prompt ("Enter the number of days growth: ");
		numDays = UserInput.readInt();
		if (numDays < 1) {

		}
		if (numDays > 50) {
		System.out.println ("Error with the number of days!");
		System.exit(-1);	
		}
		
		// calculate height and work out sale dates
		for (double sinDay = 1; sinDay <= numDays; sinDay++) {
		height = (int) growthRate * sinDay;
		metre = height / 100;
		cm = height % 100;
		if ((height >= 120) & ( height <= 160)) {
			System.out.println ("The Sunflower can be sold between days " + sinDay-- " and " + sinDay++);
		}
		
		}
		if (sinDay = numDays) {
			if (height < 120)
			System.out.println ("The sunflower cannot be sold");
		}
			if (height > 160) {
			System.out.println ("The sunflower cannot be sold");
		
		}
		
		heightTotal =+ height;

		// print results
		System.out.print ("After " + numDays + " days growth at " + growthRate + " cm a day it is now ");
		System.out.println (height + " cm tall");
		System.out.println ("This converts to " + (int) metre + " metres and " + (int) cm + " centimetres");

		// prompt for another and loop
		counter = counter++;
		System.out.println ("Plant another  Sunflower (y/n): ");
		plantAnother = UserInput.readChar();
		if (plantAnother = y) {
			main ();
		}
		if (plantAnother = n) {
			finalprint ();
		}
		if (plantAnother = N) {
			finalprint ();
		
		}
		
		// print avg height and number of sunflowers	
		int finalprint ();
		avgHeight = (height / counter);
		if (counter = 1) {
		System.out.println ("You have printed 1 sunflower");
		} else {
		System.out.println ("You have printed " + counter + " sunflowers");	
		}
		System.out.println ("The average height is " + avgHeight + " cm);
		
		}	
	} // end of main
	
} // end of class

Any help would be GREATLY appreciated guys and gals!

Recommended Answers

All 2 Replies

What is UserInput.prompt?

Use Scanner to get input from the user, unless you were directed otherwise by your instructor. For example,

Scanner userInput = new Scanner(System.in);

//System.in is connected to the keyboard by default, so the statement above will create a Scanner object called userInput that allows you to read in input. Google "Java Scanner" to see the methods it supports.

Also, friendly tip: post as many details as necessary, but don't post more. Try to identify the problem & if you can't fix it, post it on here. If you can't identify the problem, fine, just post your code and post the errors you are getting.

I've done it now, took too long lol, we haven't been taught about Scanner so I don't know if we're meant to use it, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.