Software Engineering Do While Loop

    • Thread Starter
    Offline

    0
    ReputationRep:
    Help!! I need to create a program where the user inputs height - program asks for if the user wants to input more heights? if yes you program loops back to the start - if not, drops out and gives the average of heights and asks the user if they want to continue or terminate the program??? I'm still new to the programming world, any help would be great
    Offline

    3
    ReputationRep:
    Hi I am only able to help if you need this help in java - I have v little knowledge of other languages. Also, to progress nd understand the code below, you will need to have done some simpler programming (very basic), otherwise the code will mean nothing to you! Good luck!

    Java is an object-oriented language, meaning you create a class, which is similar to a general category, and then you create an object of that class, i.e. an example or instance of that general category e.g. you might create a class called Car, and then create an object(s) of that Car class (an instance) which could be Fiat, Peugeot, Merc, BMW, etc.

    The code below creates a class called HeightInfo.

    import java.util.Scanner;public class HeightInfo { /** * @param args */ double height; double averageHeight; public HeightInfo(int h){ height = h; } public double takeInData(){ System.out.println("Enter next height if any in cm"); Scanner in = new Scanner(System.in); height = in.nextDouble(); while(in.hasNext()){ } return height; } }
    Then, to create an object of the above class, you use what is called a main method, which is in the code below (I have named the class HeightInfoApp).

    public class HeightInfoApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HeightInfo hF; int height = 0; hF = new HeightInfo(height); hF.height = height; }}
    NB: I am not going to do the whole exercise for you because I am lazy (!), and if I were to, you would become lazy!!

    You need to learn, too, so:

    1. Work out the errors above and correct them.
    2. Chk the link below to work out how to make the code count the number of height values entered (obviously, you need this number to work out average).

    http://stackoverflow.com/questions/1...of-user-inputs

    If stuck, I will help. but first put in a good shot at it yourself!
    Sorry! Might sound cruel, but you can ONLY learn coding by reading/studying AND DOING (BOTH), not one or the other, so actually, I am being kind!


    Thanks!
    Offline

    3
    ReputationRep:
    Sorry the TSR site formatting got rid of the spacing and hard returns, so I am attaching the files h-w.
    Attached Files
  1. File Type: docx HeightInfo.docx (13.7 KB, 18 views)
  2. File Type: docx HeightInfoApp.docx (13.3 KB, 21 views)
    Offline

    2
    ReputationRep:
    As with most programming problems there are numerous ways to solve it in each language - and there's a lot of languages!

    Your best off starting with writing out the process as pseudocode. Don't worry too much about the actual words, just describe the steps in simple English.

    Then start to work out how to implement each line or block using the syntax of your chosen programming language.
    If you get stuck then post what you have so far and I'm sure we'll be able to help you out...
    Offline

    3
    ReputationRep:
    What language are you using?
    Offline

    2
    ReputationRep:
    (Original post by smorgan594)
    Help!! I need to create a program where the user inputs height - program asks for if the user wants to input more heights? if yes you program loops back to the start - if not, drops out and gives the average of heights and asks the user if they want to continue or terminate the program??? I'm still new to the programming world, any help would be great
    Something along the lines of...

    total = 0
    inputValue = 0
    count = 0
    average = 0
    do {
    .....write "Enter height or -1 to quit"
    .....read inputValue
    .....total = total + inputValue
    .....count = count + 1
    } while (inputValue != -1)
    average = total/count
    write "The average height is", average

    Just make sure you don't divide by zero like I have here.
    Offline

    3
    ReputationRep:
    (Original post by macpatelgh)
    x
    Java is an object-oriented language, meaning you create a class, which is similar to a general category, and then you create an object of that class, i.e. an example or instance of that general category e.g. you might create a class called Car, and then create an object(s) of that Car class (an instance) which could be Fiat, Peugeot, Merc, BMW, etc.
    This is directly irrelevant to what op wants to know, for someone who is still working with the basic fundamentals such as loops, and selection statements, teaching them object oriented concepts is not what you should be doing. Usually first year uni students don't learn object oriented programming till the end of their first year. I doubt op has even learnt about methods.

    In addition your code is terribly written, the name of your class definitely should not be HeightInfo as that doesn't define what the class does. Nor should you have a method called takeInData which returns a double??? take in what data? Your method name is not descriptive. Also why do you have that pointless while statement? It doesn't do anything useful. To be honest you use the whole class improperly, you should remove it totally. Not to mention you did not correctly use encapsulation by omitting access modifiers for your two class level variables.

    Then, to create an object of the above class, you use what is called a main method, which is in the code below (I have named the class HeightInfoApp).
    No this is incorrect. To create an object of a class you need to instantiate it. A main method is just the entry point of the application. To be honest you should of coded everything within the main method instead, no need for a class. You over complicated this problem.
    Offline

    2
    ReputationRep:
    Beware the self-taught hackers of the internet. They write code that only they can read.
    Offline

    3
    ReputationRep:
    (Original post by smorgan594)
    Help!! I need to create a program where the user inputs height - program asks for if the user wants to input more heights? if yes you program loops back to the start - if not, drops out and gives the average of heights and asks the user if they want to continue or terminate the program??? I'm still new to the programming world, any help would be great
    You already know the program behaviour, so think about how this can be translated into an implementation. It might not come to you immediately, but just keep working on it.

    If there is anything specific you are struggling with then feel free to ask, but we don't really advocate giving out complete solutions here on TSR.

    (Original post by macpatelgh)
    X
    I agree with Async. In my opinion you should spend some time revising simple programming principles, such as the pillars of programming etc.
    Offline

    0
    ReputationRep:
    hello..
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: October 16, 2016
TSR Support Team
Poll
Which party will you be voting for in the General Election 2017?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.