Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Re: Resetting Accumulator after first itterneration

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resetting Accumulator after first itterneration

    Is there any way in Java to reset my accumulator to zero after I get all of my user data in my first go around in my loop and have it displayed. Here is a short posting of my code. After my first nursing home I want my accumulator to be zero for my next nursing home data. My accumulators are collecting all user data for my total rooms and total occupied rooms when I want it split up between nursing homes.

    <public class project3DemoMorris
    {

    public static void main(String[] args)
    {
    int buildNum; //number of buildings
    int floorNum; //number of floors
    int roomFloor; //number of rooms per floor
    int occupiedRooms; //number of occupied rooms
    int floorsCount;
    int totalRooms;
    int totaloccupiedRooms;

    //Display the heading of our program
    displayMessage();

    //Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);
    DecimalFormat formatter = new DecimalFormat ("#0%");

    totalRooms=0;
    totaloccupiedRooms=0;



    //Get the number of buildings
    System.out.println("Enter the number of buildings");
    buildNum = keyboard.nextInt();





    while (buildNum<=0)
    {

    System.out.println("Invalid entry: Number of buildings must be greater than 0.");
    System.out.print("Reenter the number of buildings:");
    buildNum = keyboard.nextInt();
    }

    for (int buildingCount = 1; buildingCount <= buildNum; buildingCount++)
    {
    System.out.print("How many floors are in the nursing home " + buildingCount +"?: ");
    floorNum = keyboard.nextInt();


    while (floorNum<=0)
    {
    System.out.println("Invalid entry: Floors must be greater than 0.");
    System.out.print("Reenter the number of floors in nursing home " + buildingCount +":");
    floorNum = keyboard.nextInt();
    }
    for (floorsCount = 1; floorsCount<=floorNum; floorsCount++)
    {

    System.out.print("How many rooms does floor" + floorsCount+"?: ");
    roomFloor = keyboard.nextInt();


    while (roomFloor<10)
    {
    System.out.println("Invalid entry: Rooms cannot be less than 10.");
    System.out.print("Reenter the number of Rooms.");
    roomFloor = keyboard.nextInt();
    }


    totalRooms+=roomFloor;

    System.out.print("How many occupied rooms are on the floor?: ");
    occupiedRooms = keyboard.nextInt();


    while (occupiedRooms<=0)
    {
    System.out.println("Invalid entry: Occupied rooms must be 0 or greater.");
    System.out.println("Reenter number of occupied rooms on the floor.");
    occupiedRooms = keyboard.nextInt();
    }


    totaloccupiedRooms+=occupiedRooms;

    // System.out.println(totaloccupiedRooms);
    //System.out.println(totalRooms);


    //Create an instance of project3Morris
    //passing the data that was entered as arguments
    //to the constructor
    project3Morris number = new project3Morris(buildNum, floorNum, roomFloor, occupiedRooms, floorsCount,totaloccupiedRooms, totalRooms );

    //Get the data from number and display it



    totalRooms=0;
    totaloccupiedRooms=0;




    System.out.println ("Nursing Home:" + number.getNumBuildings());
    System.out.println ("Total Number Of Rooms:" + number.getTotalRooms());
    System.out.println ("Total Occupied Rooms:" + number.getTotalOccupiedRooms());
    System.out.println ("Total Vacant Rooms:" + number.getVacantRooms());
    System.out.println ("Occupancy Rate:" + formatter.format
    (number.getOccupancyRate()));

    if (number.getOccupancyRate()>=.75)

    System.out.println("Occupancy Rate is High");

    else if (number.getOccupancyRate()>=.50 && number.getOccupancyRate()<=.74)

    System.out.println("Occupancy Rate is Average");

    else if (number.getOccupancyRate()<.50)

    System.out.println("Occupancy Rate is Low");


    }
    }
    }
    //Creating a method for the heading of our program
    public static void displayMessage()
    {
    System.out.println("This is the Sunshine Nursing Home Program:");>

    }


    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,122
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: Resetting Accumulator after first itterneration

    Please read the Announcement topic at the top of the sub-forum to learn how to post code in code or highlight tags and other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resetting Accumulator after first itterneration

     
     
     
    public class project3DemoMorris
    {
     
     public static void main(String[] args)
     {
       int buildNum;  //number of buildings
       int floorNum;  //number of floors
       int roomFloor;  //number of rooms per floor
       int occupiedRooms; //number of occupied rooms
       int floorsCount;
       int totalRooms;
       int totaloccupiedRooms;
     
       //Display the heading of our program
      displayMessage();
     
       //Create a Scanner object for keyboard input.
       Scanner keyboard = new Scanner(System.in);
       DecimalFormat formatter = new DecimalFormat ("#0%");
     
       totalRooms=0;
       totaloccupiedRooms=0;
     
     
     
       //Get the number of buildings
       System.out.println("Enter the number of buildings");
       buildNum = keyboard.nextInt();
     
     
     
     
     
        while (buildNum<=0)
          {
     
        System.out.println("Invalid entry: Number of buildings must be greater than 0.");
        System.out.print("Reenter the number of buildings:");
                           buildNum = keyboard.nextInt();  
        }
     
         for (int buildingCount = 1; buildingCount <= buildNum; buildingCount++)
         {
          System.out.print("How many floors are in the nursing home " + buildingCount +"?: ");
                             floorNum = keyboard.nextInt();
     
     
          while (floorNum<=0)
           {
          System.out.println("Invalid entry: Floors must be greater than 0.");
          System.out.print("Reenter the number of floors in nursing home " + buildingCount +":");
                            floorNum = keyboard.nextInt();
            }         
            for (floorsCount = 1; floorsCount<=floorNum; floorsCount++)
            {
     
           System.out.print("How many rooms does floor" + floorsCount+"?: ");
                        roomFloor = keyboard.nextInt();
     
     
          while (roomFloor<10)
          {
          System.out.println("Invalid entry: Rooms cannot be less than 10.");
          System.out.print("Reenter the number of Rooms.");
                           roomFloor  = keyboard.nextInt();
            }
     
     
         totalRooms+=roomFloor;
     
         System.out.print("How many occupied rooms are on the floor?: ");
                            occupiedRooms  = keyboard.nextInt();
     
     
          while (occupiedRooms<=0)
         {
         System.out.println("Invalid entry: Occupied rooms must be 0 or greater.");
         System.out.println("Reenter number of occupied rooms on the floor.");
                             occupiedRooms = keyboard.nextInt(); 
            }
     
     
             totaloccupiedRooms+=occupiedRooms;
     
         // System.out.println(totaloccupiedRooms);
          //System.out.println(totalRooms);
     
     
          //Create an instance of project3Morris
          //passing the data that was entered as arguments
          //to the constructor
          project3Morris number = new project3Morris(buildNum, floorNum, roomFloor, occupiedRooms, floorsCount,totaloccupiedRooms, totalRooms );
     
        //Get the data from number and display it
     
     
     
    totalRooms=0;
       totaloccupiedRooms=0;
     
     
     
     
         System.out.println ("Nursing Home:" + number.getNumBuildings());
        System.out.println ("Total Number Of Rooms:" + number.getTotalRooms());
        System.out.println ("Total Occupied Rooms:" + number.getTotalOccupiedRooms());
         System.out.println ("Total Vacant Rooms:"  + number.getVacantRooms());
         System.out.println ("Occupancy Rate:" +  formatter.format
         (number.getOccupancyRate()));
     
         if (number.getOccupancyRate()>=.75)
     
                    System.out.println("Occupancy Rate is High");
     
           else if  (number.getOccupancyRate()>=.50 && number.getOccupancyRate()<=.74)
     
                    System.out.println("Occupancy Rate is Average");
     
           else if (number.getOccupancyRate()<.50)
     
                    System.out.println("Occupancy Rate is Low");
     
     
    }
    }
    }
    //Creating a method for the heading of our program
    public static void displayMessage()
            {
            System.out.println("This is the Sunshine Nursing Home Program:");
     
            }
     
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,122
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: Resetting Accumulator after first itterneration

    I'm not sure what you mean by accumulator, but why can't you just reset all class fields to zero or as would be appropriate for each data type? Or, you can start with a fresh object with all values set to their defaults by simply reinitializing an existing object:

    myObject = new MyObject();

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resetting Accumulator after first itterneration

    I want nursing home 1 total rooms or total occupied rooms separate from nursing home 2. instead of saying 80 total rooms it should say 60. it should say 20 for the first nursing home.


    This is the Sunshine Nursing Home Program:
    Enter the number of buildings
    2
    How many floors are in the nursing home 1?: 1
    How many rooms does floor1?: 20
    How many occupied rooms are on the floor?: 15
    Nursing Home:2
    Total Number Of Rooms:20
    Total Occupied Rooms:15
    Total Vacant Rooms:5
    Occupancy Rate:75%
    Occupancy Rate is High
    How many floors are in the nursing home 2?: 3
    How many rooms does floor1?: 20
    How many occupied rooms are on the floor?: 8
    Nursing Home:2
    Total Number Of Rooms:40
    Total Occupied Rooms:23
    Total Vacant Rooms:17
    Occupancy Rate:40%
    Occupancy Rate is Low
    How many rooms does floor2?: 20
    How many occupied rooms are on the floor?: 5
    Nursing Home:2
    Total Number Of Rooms:60
    Total Occupied Rooms:28
    Total Vacant Rooms:32
    Occupancy Rate:25%
    Occupancy Rate is Low
    How many rooms does floor3?: 20
    How many occupied rooms are on the floor?: 11
    Nursing Home:2
    Total Number Of Rooms:80
    Total Occupied Rooms:39
    Total Vacant Rooms:41
    Occupancy Rate:55%
    Occupancy Rate is Average

    ----jGRASP: operation complete.

    --- Update ---

    resetting classes way over my head this is a beginner java class. were just learning to create classes.

Similar Threads

  1. help with an accumulator
    By dann in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 12th, 2013, 01:36 AM
  2. Resetting a Variable on a Day of the Month
    By HarleyRowland in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2012, 10:36 AM
  3. help on simple accumulator
    By Khalon in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 10:39 PM
  4. Java is resetting my variables to zero, and other mysterious things
    By schcrosby in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2010, 08:50 PM