5 Replies - 338 Views - Last Post: 15 May 2014 - 06:58 PM Rate Topic: -----

#1 running992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-May 14

Beginner needs advice/help with final project, due friday

Posted 14 May 2014 - 07:23 PM

Hey Folks of DreaminCode, first time poster here and I could use some last minute guidance. Iím a first semester programmer and seem to be stuck on the final project. Our professor has given us a parameter of five rules:


1) The program must have File input AND file output.
2) It must have at least one User-Defined Class.
3) Use at least one loop.
4) Allow the user to make choices for different types of functionality.
5) Use an array if possible.

After seeing this, I decided to make an informative output app. The idea behind the project is that, users from the first congressional district of California would be able to see who their public servants at command. After this they would be redirected to enter another command.

For Example:

ďPlease enter the government position youíre interested inĒ
(and the user would type) Assemblyman
Then it would pop up with: Assemblyman's information:
Name: Brian Dahle
Years of Service:18
Party: Republican
Description: County controller, until assembly election
And then it would redirect you
ďPlease enter another government position youíre interested inĒ

Iíve managed to input all of the information that needs to be displayed (such is shown with the assemblyman information above) and it runs (Hooray!) however, there is another part that Iím having a lot of trouble with. I cannot figure out how to let the user input code and for it to loop back to the beginning of the program after seeing the requested information. Iíve been messing around with it a fair amount and do know that Iíd be using the scanner keyboard for any sort of input and if/while statements for the loop at the end of the code, except I donít know if either of those work with my code or where they would fit in at all.

I really hope I can get some help.

Hereís My Main:


package politicalreps;
import java.util.Scanner;
import java.text.DecimalFormat;

public class PoliticalInfo {

     public static void main(String[] args)
     {
           
            //creating the object
       
          PoliticalReps Governor = new PoliticalReps();
          PoliticalReps Congressman = new PoliticalReps();
          PoliticalReps Senator = new PoliticalReps();
          PoliticalReps Senator2 = new PoliticalReps();
          PoliticalReps Assemblyman = new PoliticalReps();
        
               
          //Set my information up
          Governor.setName("Edmund G. Brown");
          Governor.setyearsofservice(43);
          Governor.setParty("democrat");
          Governor.setDescription("The boy governor and california native");

         
          Congressman.setName("Doug LaMalfa");
          Congressman.setyearsofservice(12);
          Congressman.setParty("Republican");
          Congressman.setDescription("A farmer who plays a large role in farm rights");

         
          Senator.setName("Barbara Boxer");
          Senator.setyearsofservice(32);
          Senator.setParty("Democrat");
          Senator.setDescription("A junior senator who is in charge of security");
               
                Senator2.setName("Dianne Feinstein");
          Senator2.setyearsofservice(52);
          Senator2.setParty("Democrat");
          Senator2.setDescription("San Francisco's best mayor turned senator");
               
                 Assemblyman.setName("Brian Dahle");
                 Assemblyman.setyearsofservice(18);
                 Assemblyman.setParty("Republican");
                 Assemblyman.setDescription("County controller, until assembly election");
                
          //Printing all of my statements
               
          System.out.println("Governors Info: ");
          System.out.println("Name: "+ Governor.getName());
          System.out.println("Years of Service: " +Governor.getYearsofservice());
          System.out.println("Party: " + Governor.getParty());
          System.out.println("Description: " + Governor.getDescription());
              
          System.out.println("\nCongressman's information: ");
          System.out.println("Name: "+ Congressman.getName());
          System.out.println("Years of Service: "+ Congressman.getYearsofservice());
          System.out.println("Party: " + Congressman.getParty());
          System.out.println("Description: " + Congressman.getDescription());

         
          System.out.println("\nSenator's information: ");
          System.out.println("Name: "+ Senator.getName());
          System.out.println("Years of Service:" + Senator.getYearsofservice());
          System.out.println("Party: " + Senator.getParty());
          System.out.println("Description: " + Senator.getDescription());
               
                System.out.println("\nSenator2's information: ");
          System.out.println("Name: "+ Senator2.getName());
          System.out.println("Years of Service:" + Senator2.getYearsofservice());
          System.out.println("Party: " + Senator2.getParty());
          System.out.println("Description: " + Senator2.getDescription());
               
                System.out.println("\nAssemblyman's information: ");
          System.out.println("Name: "+ Assemblyman.getName());
          System.out.println("Years of Service:" + Assemblyman.getYearsofservice());
          System.out.println("Party: " + Assemblyman.getParty());
          System.out.println("Description: " + Assemblyman.getDescription());
                             

     }
}





And Hereís the other portion of my code:


package politicalreps;
import java.util.Scanner;
import java.text.DecimalFormat;
public class PoliticalReps
{

        private String name;
     private String party;
     private int yearsofservice;
     private String description;

     /**
     The no arg constructor initializes an empty object
      */

     public PoliticalReps()
     {
          name = "";
          party = "";
          yearsofservice= 0;
          description = "";

     }

     /**
     The parameterized constructor accepts arguments for the object's fields.
          @paran theirName= A person's name.
          @paran theirParty= A politician's political party.
          @paran theirYearsofservice= A person's age.
          @paran theirDescription= A person's phone number.

      */


     public PoliticalReps(String theirName, String theirParty, int theirYearsofservice, String theirDescription)
     {
          name= theirName;
          party= theirParty;
          yearsofservice= theirYearsofservice;
          description= theirDescription;
     }
       
     
     /**
        The setName method sets the person's name
        @param theirName = The Person's name
      */

     public void setName(String theirName)
     {
          name= theirName;
     }
       
      
       
        /**
        The setParty method sets the person's party leaning.
        @param theirParty = The candidate's party
      */
     public void setParty (String theirParty)
     {
          party= theirParty;
     }

 
      
        /**
        The setyearsofservice method sets the persons public service tenure.
        @param theirYearsofservice = Amount of public service provided by candidate
      */
     public void setyearsofservice(int theirYearsofservice)
     {
          yearsofservice= theirYearsofservice;
     }


        /**
        The setPhone method sets the person's phone number   
         @param theirDescription Candidate's description. 
      */
     public void setDescription (String theirDescription)
     {
                description = theirDescription;
        }
        /**______________________________________________________*/
      
     /**
        The getName method returns the person's name
        @return the candidate's name
      */
     public String getName()
     {
          return name;
     }
       
       
       
        /**
        The getParty method returns the candidate's Party
         @return Party affiliation(Democrat, Republican, or Independent)
      */
     public String getParty()
     {
          return party;
     }
       
       

     /**
        The getYearsofservice method returns the candidate's public service record
         @return public service record.
      */
     public int getYearsofservice()
     {
          return yearsofservice;
     }

    

     /**
         The getDescription method returns the candidate's biography and why they
        * have decided to run. 
       @return The person's phone number 
      */
     public String getDescription()
     {    
          return description;
     }
}
 


Thank's Again.

Is This A Good Question/Topic? 0
  • +

Replies To: Beginner needs advice/help with final project, due friday

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Beginner needs advice/help with final project, due friday

Posted 14 May 2014 - 09:29 PM

Think about your loop condition. While the user wants to input another member, allow user to input another party member:
boolean isDone = false;

while(!isDone){  // while the user is not done entering info
   //code to handle input of party member

}



Now if you want to use an array, you could store a PoliticalRep[] and have the user enter up to n PoliticalReps:
System.out.print("How many reps do you want to enter? ");
int numReps = scan.nextInt();
scan.nextLine();

PoliticalReps[] representatives = new PoliticalReps[numReps];

for(int i = 0; i < representatives.length; i++){
    //have the user enter the information for a single PoliticalReps object
    //and store that object in representatives[i]
}


Was This Post Helpful? 0
  • +
  • -

#3 running992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-May 14

Re: Beginner needs advice/help with final project, due friday

Posted 15 May 2014 - 06:16 PM

Thanks so much for the response,

I tried implementing as you said to. From examples I found online it looked as if I could combine the array and while statement so I ended up with this
package politicalreps;
import java.util.Scanner;
public class PoliticalInfo {

	public static void main(String[] args)
	{
            
                
           
            
            //creating the object
          PoliticalReps Governor = new PoliticalReps();
          PoliticalReps Congressman = new PoliticalReps();
          PoliticalReps Senator = new PoliticalReps();
          PoliticalReps Senator2 = new PoliticalReps();
          PoliticalReps Assemblyman = new PoliticalReps();
          
         
                
		//Set my information up
		Governor.setName("Edmund G. Brown");
		Governor.setyearsofservice(43);
		Governor.setParty("democrat");
		Governor.setDescription("The boy governor and california native");

		
		Congressman.setName("Doug LaMalfa");
		Congressman.setyearsofservice(12);
		Congressman.setParty("Republican");
		Congressman.setDescription("A farmer who plays a large role in farm rights");

		
		Senator.setName("Barbara Boxer");
		Senator.setyearsofservice(32);
		Senator.setParty("Democrat");
		Senator.setDescription("A junior senator who is in charge of security");
                
                Senator2.setName("Dianne Feinstein");
		Senator2.setyearsofservice(52);
		Senator2.setParty("Democrat");
		Senator2.setDescription("San Francisco's best mayor turned senator");
                
                 Assemblyman.setName("Brian Dahle");
                 Assemblyman.setyearsofservice(18);
                 Assemblyman.setParty("Republican");
                 Assemblyman.setDescription("County controller, until assembly election");
                 
		//Printing all of my statements
                 
                
                
		System.out.println("Governors Info: ");
		System.out.println("Name: "+ Governor.getName());
		System.out.println("Years of Service: " +Governor.getYearsofservice());
		System.out.println("Party: " + Governor.getParty());
		System.out.println("Description: " + Governor.getDescription());
               
		System.out.println("\nCongressman's information: ");
		System.out.println("Name: "+ Congressman.getName());
		System.out.println("Years of Service: "+ Congressman.getYearsofservice());
		System.out.println("Party: " + Congressman.getParty());
		System.out.println("Description: " + Congressman.getDescription());

		
		System.out.println("\nSenator's information: ");
		System.out.println("Name: "+ Senator.getName());
		System.out.println("Years of Service:" + Senator.getYearsofservice());
		System.out.println("Party: " + Senator.getParty());
		System.out.println("Description: " + Senator.getDescription());
                
                System.out.println("\nSenator2's information: ");
		System.out.println("Name: "+ Senator2.getName());
		System.out.println("Years of Service:" + Senator2.getYearsofservice());
		System.out.println("Party: " + Senator2.getParty());
		System.out.println("Description: " + Senator2.getDescription());
                
                System.out.println("\nAssemblyman's information: ");
		System.out.println("Name: "+ Assemblyman.getName());
		System.out.println("Years of Service:" + Assemblyman.getYearsofservice());
		System.out.println("Party: " + Assemblyman.getParty());
		System.out.println("Description: " + Assemblyman.getDescription());
                
                
                    
              boolean isDone = false;
              while(!isDone){ 
              System.out.print("How many reps do you want to enter? ");
              int = System.nextInt(); System.nextLine();
              PoliticalReps[] representatives = new PoliticalReps[numReps];
              
              for(int i = 0; i < representatives.length; i++){    //have the user
                    //enter the information for a single PoliticalReps object   
                    //and store that object in representatives[i] 
                }
                }
                
                
                
                  


	}
}




The errors that I'm receiving are "cannot find symbol" for "PoliticalReps[] representatives = new PoliticalReps[numReps];" and I'm not sure where I'm supposed to define "numReps" and on the last portion of my code, that you helped me with, the errors are on the lines "int = System.nextInt(); System.nextLine();" and right there it's saying that nextInt and nextLine cannot be found, even though I already imported the scanner.

Thanks,


View Postmacosxnerd101, on 14 May 2014 - 09:29 PM, said:

Think about your loop condition. While the user wants to input another member, allow user to input another party member:
boolean isDone = false;

while(!isDone){  // while the user is not done entering info
   //code to handle input of party member

}



Now if you want to use an array, you could store a PoliticalRep[] and have the user enter up to n PoliticalReps:
System.out.print("How many reps do you want to enter? ");
int numReps = scan.nextInt();
scan.nextLine();

PoliticalReps[] representatives = new PoliticalReps[numReps];

for(int i = 0; i < representatives.length; i++){
    //have the user enter the information for a single PoliticalReps object
    //and store that object in representatives[i]
}


Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 671
  • View blog
  • Posts: 2,953
  • Joined: 03-December 12

Re: Beginner needs advice/help with final project, due friday

Posted 15 May 2014 - 06:39 PM

Either use a sentinel loop OR. A counting loop. Not both.


Quote

088. †int = System.nextInt();


You are not defining a variable here just trying to assign a value to a type.


By file input : are you supposed to be reading from a file? That would be more useful than hardcoding the data in, you would also use an array to assign the data to the setters of you class.
Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Beginner needs advice/help with final project, due friday

Posted 15 May 2014 - 06:51 PM

Note as well: the array *must* be declared outside of the loop to be of any use. Local variables are not preserved. That is, since you declared the array inside the loop, it will be garbage collected at the end of the loop iteration. Declaring (and initializing) it outside of the loop will fix this.

Also, all your input code should go inside the loop, not outside the loop. That's the whole point of using a loop.
Was This Post Helpful? 2
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 671
  • View blog
  • Posts: 2,953
  • Joined: 03-December 12

Re: Beginner needs advice/help with final project, due friday

Posted 15 May 2014 - 06:58 PM

You are also repeating yourself with the output. Provide a toString method for output with the variables needed. You might also want to add the chair title to the class and not use it as a name. You could then use an if statement to link the index number of the representative array to the person.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1