Results 1 to 8 of 8
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default WeightLoss specification java programm

    task

    calculate a userís weight loss based on the amount of exercise they take
    user specifies full name and hours spent cycling, running and swimming
    output first name, total calories burnt and pounds lost

    task cals burnt per hour
    cycling 200
    running 475
    swimming 275


    person loses 1 pound for every 3500 calories burnt




    //calculate and output weight loss given time spent on activities
    import java.util.*;

    public class WeightLoss
    {
    public static void main (String args[])
    {
    //create Scanner for input
    //declare constants for calories per activity and calories burnt
    //prompt for name and input response
    //prompt for hours spent exercising and input responses
    //determine first name
    //calculate calories burnt
    //calculate weight loss
    //output name, calories burnt and weight loss
    }
    }

    this is what i have got so far

    import java.util.Scanner;

    public class weightlossprogram {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    final int CYCLING__CALS = 200;
    final int RUNNING__CALS = 475;
    final int SWIMMING__CALS = 275;

    System.out.print("Cycling Cals: ");
    int Cycling = sc.nextInt();

    System.out.print("Running Cals: ");
    int Running = sc.nextInt();

    System.out.print("Swimming Cals: ");
    int Swimming = sc.nextInt();

    but i need help from here with the maths bit please

    many thanks Andy..

  2. #2
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: WeightLoss specification java programm

    ok i worked the maths out i think

    int totalLbsLostFromCycling = ((Cycling * 200)/3500);
    int totalLbsLostFromRunning = ((Running * 475)/3500);
    int totalLbsLostFromSwimming = ((Swimming * 275)/3500);

    but could somebody help me put it all together so it gives me a output

    many thanks Andy..

  3. #3
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: WeightLoss specification java programm

    You've done all of the hard work. Simply bundle that into the main method and at the end use System.out.println() to show the results.

    For instance:
    Java Code:
    System.out.println(totalLbsLostFromCycling);
    Regards Serb.

  4. #4
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: WeightLoss specification java programm

    thankyou very much

    Andy...

  5. #5
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: WeightLoss specification java programm

    ok i think there is an error some where as i get zero when i entre these details

    also how would i get the weight loss for this please

    H:\FPT>java weightlossprogram
    Cycling Cals: 10
    Running Cals: 2
    Swimming Cals: 2
    total pounds lost from Cycling is 0
    total pounds lost from Running is 0
    total pounds lost from Swimming is 0

    many thanks Andy

  6. #6
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: WeightLoss specification java programm

    sorry and this is my code


    import java.util.Scanner;

    public class weightlossprogram
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    final int CYCLING__CALS = 200;
    final int RUNNING__CALS = 475;
    final int SWIMMING__CALS = 275;

    System.out.print("Cycling Cals: ");
    int Cycling = sc.nextInt();

    System.out.print("Running Cals: ");
    int Running = sc.nextInt();

    System.out.print("Swimming Cals: ");
    int Swimming = sc.nextInt();

    int totalLbsLostFromCycling = ((Cycling * 200)/3500);
    int totalLbsLostFromRunning = ((Running * 475)/3500);
    int totalLbsLostFromSwimming = ((Swimming * 275)/3500);

    System.out.println ("total pounds lost from Cycling is " + (totalLbsLostFromCycling));
    System.out.println("total pounds lost from Running is " + (totalLbsLostFromRunning));
    System.out.println("total pounds lost from Swimming is " +(totalLbsLostFromSwimming));
    }
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: WeightLoss specification java programm

    You're doing int division which always returns an int. so 9/4 returns 2, 7/4 returns 1, and 3/4 returns 0. If you need to do floating point arithmetic, use doubles. For instance 3.0/4.0 returns 0.75 or very close to it.

  8. #8
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: WeightLoss specification java programm

    ok thankyou very much for that solution i have the maths sorted out

    the one thing i am stuck on is i have to entre a name before i entre results first and second and then when i entre the cals i have to have the first name appere before results

    so it will have to say entre name ..........

    then i put cals in ........

    and then it will then output first name with results.

    many thanks Andy

    this is my finished code so far with out name


    import java.util.Scanner;

    public class weightlossprogram
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    final int CYCLING_CALS = 200;
    final int RUNNING_CALS = 475;
    final int SWIMMING_CALS = 275;




    System.out.print("Cycling hours: ");
    int cycling = sc.nextInt();

    System.out.print("Running hours: ");
    int running = sc.nextInt();

    System.out.print("Swimming hours: ");
    int swimming = sc.nextInt();

    int caloriesFromCycling = cycling * CYCLING_CALS;
    int caloriesFromRunning = running * RUNNING_CALS;
    int caloriesFromSwimming = swimming * SWIMMING_CALS;

    double calories = caloriesFromSwimming + caloriesFromRunning + caloriesFromCycling;
    double poundsLost = calories / 3500;

    System.out.println ("total pounds lost is " + poundsLost);
    System.out.println("total calories burnt is " + calories);

    }
    }

Similar Threads

  1. Java Programm for ssh connection
    By sanaline in forum New To Java
    Replies: 3
    Last Post: 05-31-2011, 01:09 PM
  2. How to create an executable from my java programm
    By Chaosje in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 04:32 PM
  3. Replies: 47
    Last Post: 01-09-2011, 09:53 PM
  4. Replies: 8
    Last Post: 02-17-2009, 01:57 PM
  5. latest java class specification
    By underthesun in forum Advanced Java
    Replies: 1
    Last Post: 08-28-2008, 09:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •