Inheritance Program

Problem with Strings in program

Page 1 of 1

2 Replies - 3848 Views - Last Post: 02 November 2008 - 03:42 PM Rate Topic: -----

#1 bigdavemtz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 08

Inheritance Program

Posted 01 November 2008 - 07:52 PM

Hello I am doing an Inheritance Program for my CS lab and I am getting four errors that I don't know how to fix please help!!!

The Errors:
java:69: class, interface, or enum expected
public String FirstName(){

java:71: class, interface, or enum expected
}

java:73: class, interface, or enum expected
public String LastName(){

java:75: class, interface, or enum expected
}
^


import java.util.Scanner;
import java.io.*;
enum data_type{GPA, HOURS}
enum relation_type{FATHER,MOTHER,SON,DAUGHTER}

//Making the classes needed for program

	class Contact
   {
	String FirstName = new String();
	String LastName = new String();
	Phone phone = new Phone();
   
   }
   
   class SchoolContact extends Contact
   {
   int ID;
   Data data = new Data();
   }
   
   class FamilyContact extends Contact
   {
   relation_type r;
   }
   class  Phone
   {
	
	 int area_code=0;
	 int prefix=0;
	 int suffix=0;
   }
  
   class Data
   {
   
		data_type x;
		private float GPA=0;
		private int Hours=0;
	
	// Method for storing my values
		public void storing (){
		Scanner keyboard = new Scanner(System.in);
			if (x == data_type.GPA){
			GPA = keyboard.nextFloat();
			}else{
				Hours = keyboard.nextInt();
				}
			}
	
			public float return_GPA(){
				return GPA;
			}
		
			public int return_Hours(){
				return Hours;
			}
   }
	
		
	public String FirstName(){
		return FirstName;
		}
		
	public String LastName(){
		return LastName;
		}


public class lab7
{

  public static void main(String[] args)
	{
  Scanner keyboard = new Scanner(System.in);
  int  user_choice=0, length=0, dislength=0, i=0;
  char choice;
  
	Contact material[] = new Contact[3];
	for( i=0; i<3; i++){
	  material[i] = new Contact();
	  }
  
	  System.out.println("David Martinez");
	  System.out.println("Lab Section 1");
	  System.out.println("Lab # 5");
	  System.out.println("Lab Date 20080907");
	  
	 do{
	 
	  System.out.println("1)Enter a new record:");
	  System.out.println("2)Display the records");
	  System.out.println("3)Quit");
	 
	  user_choice = keyboard.nextInt();
	  
	switch (user_choice){
	
		case 1:
	
			if (length <3){
				System.out.print("Enter students ID number: 900");
				material[length].ID=keyboard.nextInt();
				
				System.out.println("G)Enter the GPA for the student:");
				System.out.println("H)Enter this contacts completed credited hours.");
				choice = keyboard.next().charAt(0);
				
					switch (choice){
						case 'g':
						case 'G':
							System.out.println("GPA:");
							material[length].data.x=data_type.GPA;
							material[length].data.storing();
							break;
						case 'h':
						case 'H':
							System.out.println("Hours:");
							material[length].data.x=data_type.HOURS;
							material[length].data.storing();
							break;
						default:
							System.out.println("User error please try again");
							}
				System.out.print("Enter the Phone Number of this student: (###)-###-####:");
				material[length].phone.area_code=keyboard.nextInt();
				material[length].phone.prefix=keyboard.nextInt();
				material[length].phone.suffix=keyboard.nextInt(); 
			}
				else{
					System.out.println("The list is full");
					}
				length++;
				break;

		case 2:
			for(dislength=0; dislength < length; dislength++){
				System.out.print("\n 900" + material[dislength].ID);
				
			if (material[dislength].data.x==data_type.GPA){
				
				System.out.println("\n GPA:"+ material[dislength].data.return_GPA());
				}
			
			
			else {				
				System.out.println(" \n HOURS:" + material[dislength].data.return_Hours());
				}
				
			
				System.out.println("("+ material[dislength].phone.area_code+ ") " +material[dislength].phone.prefix + "-"+ material[dislength].phone.suffix);
				
			
				
						
			}break;
			
		case 3:
			System.out.println("Laterz");
				break;

		}
	   
	  }while(user_choice != 3);
   
 } 
  

}



Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance Program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Inheritance Program

Posted 01 November 2008 - 08:09 PM

Ok, you have a quite a bit of problems with this. So lets go through some of them here.

1) In the Data class you are missing some closing curly braces. One to close off the function "storing" and another to end the actual Data class.

2) You have two functions that look like they were suppose to be part of Data called "FirstName()" and "LastName()" but those should actually be up in the class "contact" because that is where you have those member variables (FirstName and LastName).

3) You create an array of Contact items called "material" where you try to access fields like ID, data etc. Remember you created it as an array of CONTACT objects, those objects don't know anything about ID or Data. Even if you were to store one of the other objects into that array of Material (where they would know about these fields) you still couldn't access them because Java can only assume that all objects in the material array are of type Contact.

So for instance...

// Here Java always assumes that they are AT LEAST type contact and what
// a contact supports (like firstname and lastname)
Contact material[] = new Contact[3];

// Lets say we created another type of contact and stored it into material

// This is legal because schoolcontact is a type of contact object...

Contact material[] = new SchoolContact[3];

// However, java still has to treat the array as an array of Contact objects // even if the objects can do so much more

SchoolContact schoolcontactarray[] = new SchoolContact[3];

// Now we could get access to the additional attributes of a school contact

// Illegal! A school contact is a type of contact, but not all contacts are a type
// of school contact
SchoolContact schoolcontactarray[] = new Contact[3];



Hopefully that makes sense. Fix these three problems and you will be on the right track here with this. If you continue to have problems, show us what you have updated.

:)
Was This Post Helpful? 0
  • +
  • -

#3 bigdavemtz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 08

Re: Inheritance Program

Posted 02 November 2008 - 03:42 PM

Thanks alot for your help Martyr2 all of the errors I was getting are gone. I will post again if i need an additional help on my program.
Thanks>>bigdavemtz
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1