Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2012, 02:39 PM   PM User | #1
MJCoding
New to the CF scene

 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
MJCoding is an unknown quantity at this point
Coding issues in creating program that selectively outputs data from a tex

Hi all,
I would greatly appreciate any help I could get with this program (which is described below) , together with the code I’ve written, as I’m getting a lot of errors in the code which suggests I’m seriously off track. Thanks very much.
__________________________________________________________________________________
Program:
The city periodically goes through its records and schedules property inspections (e.g., for lead or rodents). You will complete a Java application that reads a list of property data from a file and determines what kinds of inspections should be scheduled (if any). When properties contain multiple units (e.g., apartments), an inspection is scheduled for the entire building; however, the types of inspections are determined based on data concerning each unit.

Output of the completed program should look like (sample run, user input in bold):

Owner Street Unit Built
Bill Jannings 1 College Way 2000
Danielle Parsons 1200 Beacon St. 7 1998
Terry Hawkings 1200 Beacon St. B 1999
Greg Royal 1200 Beacon St. 17 1998
Dennis Howe 15 Boltoph St. 1 1885
Soi Fong 15 Boltoph St. 2 1885
William Burrows 15 Boltoph St. 5 1885
Ken Dahal 247 Harvard Ave. 1899
Inspections:
1 College Way:
1200 Beacon St.: rodents
15 Boltoph St.: lead rodents
247 Harvard Ave.: lead

Outline
1. Reads all property data from file (properties.txt) into array.
2. Displays property data: owner name, street address, unit (if any), and year built.
3. Displays the inspections scheduled for each building.

Property Class
The Property class represents data for a single property within the city.
Data Members
The code provided includes the following data members:
• owner: owner’s name.
• streetAddress: street address without the unit number.

•unit: unit information (may be in various formats: “Unit #B”, “Apt. B”, etc.).
• builtYear: year unit was built (or when building was built, if there aren’t separate units).
Methods
The code provided includes a default constructor as well as some accessors and mutators. You must complete 3 methods within the class (headers below):
1. Method to set the address:

public void setAddress(String newAddress)
This method should break the address passed into its street address and unit. Assume the unit (if any) follows a comma (,). If there is no unit listed, store the empty string as the unit. Look up appropriate String class methods to find the comma in a string, etc.
2. Constructor to initialize the object using the values passed:

public Property(String initOwner,
String initAddress,
int initBuiltYear)

This constructor should initialize all data members using its parameters. As part of its definition, call method setAddress (see above) to set the address, thus initializing the street address and unit members. Currently, the default constructor and “set” methods are used in method loadProperties [Inspections.java] to initialize each property record stored in an array. Change the code to instead use this constructor.
3. Method to extract the number (or letter, etc.) of the unit:

public String getUnitNum()
This method should return a string containing only the unit number. For example, units are stored in various ways within the property data file: Apt. 7, Unit #2, simply #17, or even just B. This method should not change how the unit is stored within the Property class, it should only extract and return the unit number (here, 7, 2, 17, and B) and returns it.
This will again require using String class methods. Examine the property data file properties.txt to see all the formats used. This method is called from method displayProperties [Inspections.java].
Main Program (Class Inspections)
The main program (class Inspections) is divided into methods. Notably, method loadProperties returns an array. Recall that a Java array is stored using a reference. A couple methods list throws clauses (throws FileNotFoundException) since opening a file may cause an exception that the code does not catch.
As stated in 2. under “Methods” above, in method loadProperties, instantiate each Property object using the constructor (with arguments) that you wrote [Property.java].
Complete method scheduleInspections to display scheduled inspections for buildings. The method will not list inspections for every unit, only for each building (unique street address). Call that method from main.
For example, reviewing the sample run:
Owner Street Unit Built
Bill Jannings 1 College Way 2000
Danielle Parsons 1200 Beacon St. 7 1998
Terry Hawkings 1200 Beacon St. B 1999
Greg Royal 1200 Beacon St. 17 1998
Dennis Howe 15 Boltoph St. 1 1885
Soi Fong 15 Boltoph St. 2 1980
William Burrows 15 Boltoph St. 5 1885
Ken Dahal 247 Harvard Ave. 1899
Inspections:
1 College Way:
1200 Beacon St.: rodents
15 Boltoph St.: lead rodents
247 Harvard Ave.: lead
The schedule does not show “1200 Beacon St.” multiple times; instead it appears only once for its 3 units. Below outlines how method scheduleInspections displays output for each building only once:
• Declares a variable to keep track of the last street address seen.
• Loops through each of the properties.
• If a property’s street address is the same as the last street address seen, then we must be in the same building (assumes property records for a particular building are contiguous). Note how it correctly compares strings.
• If the street address changes, outputs the address of the building (right now it doesn’t output any inspections).

You must add code to determine and output inspections as follows:
• If any unit in a building was built before 1970, schedule a “lead” inspection. Add an appropriate constant.
• If there is more than one unit in a building, schedule a “rodent” inspection.
• Eliminate the spurious output that appears for the first property record.
To determine these inspections, you must track the earliest (minimum) “year built” for each building (among all its units). In addition, your must count the number of units in a building.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

My ( error-filled ) code is as follows. It contains a parent (main) class called Inspections & a child class called Property .

(a) Inspections (main):

Code:
/*
 * File: Inspections.java
 * Date: Monday, September 17th 2012.
 * Description: A Java application,
 * that reads a list of property data from a file,
 * and determines what kinds of inspections should be scheduled (if any). 
 */
 
import java.util.*;  // for Scanner
import java.io.*;    // for FileReader

public class Inspections
   {
   // Load all property records from file.
   public static void  loadProperties()
   throws FileNotFoundException   // unhandled exception for FileReader

   {
   String[] owner = new String[8];
   String[] streetAddress = new String[8];
   String[] unit = new String[8];
   int[] builtYear = new int[8];
   
   }
      


   public static Scanner console = new Scanner(System.in);
   
   public static void main(String[] args)
      throws FileNotFoundException
   {
      Property[] allProperties = loadProperties(Property.Properties);
      
      // CALL METHOD scheduleInspections
         scheduleInspections( );

      
      displayProperties(allProperties);
      
      // Allow user to enter names of properties
      
      System.out.print("Enter name of property:  ");
      String name = console.nextLine( );
      
      Property property1 = new Property(name);
      System.out.println("Initial status: " + property1);
      
      
      // Loop through each of the properties
      final int REQUIREMENT_FOR_RODENT_INSPECTION = 2;

      while(property1.getScheduleInspections( ) > 0)
      {
       // Determine if inspection required
       if(property1.getUnit()< REQUIREMENT_FOR_RODENT_INSPECTION)
       {
        System.out.println("Schedule rodent inspection");
        }
        else if (property1.getYear() < 1970)
        {
         System.out.println("Schedule lead inspection");
         }
                  
      
      Scanner condFile = new Scanner(new FileReader("properties.txt"));

      int numPropertys = condFile.nextInt();  // # of records in file

      // Create array large enough for all records in file.
      Property[] readProperties = new Property[numPropertys];
      
      // Read in all weather property data.
      for (int i = 0; i < readProperties.length; i++)
      {
         condFile.nextLine();  // eat newline since just read number
         String owner = condFile.nextLine();
         String address = condFile.nextLine();
         int year = condFile.nextInt();
         
         // REPLACE DEFAULT CONSTRUCTOR AND "set" CALLS WITH
         // USE OF CONSTRUCTOR THAT TAKES ARGUMENTS.
         readProperties[i] = new Property(owner,address,year);  //readProperties[i] = new Property();
           //readProperties[i].setOwner(owner);
         //readProperties[i].setAddress(address);
         //readProperties[i].setBuiltYear(year);
      }
      
      return readProperties;
   }
   
// Display formatted  data......started  here
         }
      }
   //}
   
      // Display formatted data for each property.
     public static void displayProperties(Property[] showProperties)  //
   {
      System.out.printf("%-20s%-25s%-9s%-7s%n",
         "Owner", "Street", "Unit", "Built");
   //}
         
      for (int i = 0; i < showProperties.length; i++)
      {
         System.out.printf("%-20s%-25s%-9s%-7d%n",
            showProperties[i].getOwner(),
            showProperties[i].getStreetAddress(),
            showProperties[i].getUnitNum(),
            showProperties[i].getBuiltYear() );
      }
   }
   
   // Display needed inspections for each building.
      String rodent;
      String lead;
   // COMPLETE METHOD scheduleInspections
// public static void scheduleInspections(Property[] inspectProperties)
   {
     String lastAddress = "";

      System.out.println("\nInspections:");

      for (int i = 0; i < inspectProperties.length; i++)
      {
         String currAddress = inspectProperties[i].getStreetAddress();

         if (!currAddress.equals(lastAddress))
         {
            System.out.print(lastAddress + ":");
            System.out.println();
            
            lastAddress = currAddress;
         
         }}}
   }  
   }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(b) Property (child) class:
Code:
/*
 * File: Property.java
 * Date: Monday 24th September 2012.
 * Description: Property class is a subclass of Inspections.
 */

import java.util.*;  // for Scanner

public class Property
{
   private String owner;
   private String streetAddress;
   private String unit;
   private int builtYear;
   
   // "default constructor" called when no arguments
   // given during instantiation.
   public Property()
   {
      owner = "";
      streetAddress = "";
      unit = "";
      builtYear = 0;
   }
   
   // ADD A CONSTRUCTOR THAT EXPECT ARGUMENTS WITH
   // INITIAL VALUES FOR DATA MEMBERS.
   
   public Property(String initOwner, String initAddress, int initBuiltYear)
   {
       owner = initOwner;
       setAddress(initAddress);     
       builtYear = initBuiltYear;
   }


   // "accessor" methods to give back values of data members.
   
   public String getOwner()
   {
      return owner;
   }
   
   public String getStreetAddress()
   {
      return streetAddress;
   }

   public String getUnit()
   {
      return unit;
   }

   public int getBuiltYear()
   {
      return builtYear;
   }

   public String getUnitNum()
   {
      // ADD CODE TO EXTRACT AND RETURN UNIT NUMBER.
      String num = "";
      for(int i = 0; i < unit.length; i++)
      {     
         if(Character.isDigit(unit[i].charAt(0)))
        {
            num = num + unit[i];
         }
         else
         {
         Character.isDigit(unit[i].charAt(unit.length - 1));
         }    
     }
      return num;
   }

   // "mutator" methods to change values of data members.
      
   public void setOwner(String newOwner)
   {
      owner = newOwner;
   }
   
   public void setAddress(String newAddress)
   {
      // ADD CODE TO SEPARATE AND STORE THE
      // STREET ADDRESS AND UNIT.
     String [] newAddressSplit = newAddress.split(",",2);
     
     streetAddress = newAddressSplit[0];
     unit = newAddressSplit[1];
    }
      
   public void setBuiltYear(int year)
   {
      builtYear = year;
   }
   
    public void getScheduleInspections( )         // called from main
    {
      return scheduleInspections;
    }

}
MJCoding is offline   Reply With Quote
Old 09-22-2012, 03:13 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
See:- http://www.codingforums.com/showthread.php?t=17515.

You will not get any reply in this forum which is intended to be used only to
post a completed (working) script for showcasing/benefit of others.

Also, this is the (Post A) JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 09-22-2012, 06:04 PM   PM User | #3
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,592
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Moved to appropriate forum.
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 09-22-2012, 06:17 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is clearly a homework assignment. While we can assist you on homework assignments, we cannot just write the code and give it to you.
Please start by posting the errors themselves. Are the compilation errors or logical errors? If they are logical errors, post the input provided, the output received, the expected output.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:19 PM.


Advertisement
Log in to turn off these ads.