Results 1 to 5 of 5

Thread: Help

  1. #1
    marc is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Help

    THIS IS MY CODE
    i tried to do it but i couldn't

    Write a program to read 2 students full names starting with first then fathers name then family , (separated by blanks , e.g : ashelly joe daniel ), then :

    If these two students have same full names display ( These are duplicate names) .
    If they are brothers display (They are brothers or sisters)
    If they are only from same family , display (They are relatives)
    If none of the above , display (May be , they are just friends).

    Note : find a way to avoid case-sensitivity, because some will enter names capital or small letters , but for us it should be the same. ( ashelly == ASHELLY)



    Java Code:
    import java.util.*;
    public class MARC
    {
    static Scanner console = new Scanner(System.in);
    public static void main(String [] args)
    {
    
    String name1, name2;
    int apos1, apos2, rpos1, rpos2;
    String first, second, father1, father2, family1, family2;
    
    System.out.println("enter first name: ");
     name1 = console.nextLine();
     
     System.out.println("enter second name: ");
     name2 = console.nextLine();
     
     apos1 = name1.indexOf(' ');
    apos2 = name1.indexOf(' ', apos1);
     
     rpos1 = name2.indexOf(' ');
     rpos2 = name2.indexOf(' ', rpos1);
     
     first = name1.substring(0, apos1);
     father1 = name1.substring(apos1+1,apos2+1);
     family1 = name1.substring(apos2);
     
      second = name2.substring(0, rpos1);
      father2 = name2.substring(rpos1+1, rpos2+1);
      family2 = name2.substring(rpos2);
     
     if
     (first.equals(second))
     {
       if 
    	(father1.equals(father2))
    	
    	   if
    		(family1.equals(family2))
     System.out.println("These are duplicate names");
     
     
    else if
     (first.equals(second))
            if
     (father1.equals(father2))
               if
    	 (family1.equals(family2))
    	 
    	 System.out.println("They are brothers or sisters");
    
    
    else if 
     (family1.equals(family2))
     System.out.println("They are relatives");
     
     }
     
     else
     System.out.println("May be , they are just friends");
     
     
     }}

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
     if
     (first.equals(second))
     {
       if 
    	(father1.equals(father2))
    	
    	   if
    		(family1.equals(family2))
     System.out.println("These are duplicate names");
    use this
    if(first.equals(second) && father1.equals(father2) && family1.equals(family2));

    Java Code:
    else if
     (first.equals(second))
            if
     (father1.equals(father2))
               if
    	 (family1.equals(family2))
    	 
    	 System.out.println("They are brothers or sisters");
    use this:
    else if
    (!first.equals(second) && father1.equals(father2) && family1.equals(family2));

    I'll try to write a better code later.

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Note : find a way to avoid case-sensitivity, because some will enter names capital or small letters , but for us it should be the same. ( ashelly == ASHELLY)
    my opinion
    change every
    console.nextLine();
    with
    console.nextLine().toLowerCase();

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I wrote some code. I think it works.

    Java Code:
    import java.util.Scanner;
    
    public class CompareStudents {
    	
    	
    	
    	public static void main(String[] args) {
    		Scanner console = new Scanner(System.in);
    		String consoleFirstName;
    		String consoleSecondName;
    		String consoleFirstFatherName;
    		String consoleSecondFatherName;
    		String consoleFirstFamilyName;
    		String consoleSecondFamilyName;
    		
    		consoleFirstName = console.nextLine().toLowerCase();
    		consoleSecondName = console.nextLine().toLowerCase();
    		consoleFirstFatherName = console.nextLine().toLowerCase();
    		consoleSecondFatherName = console.nextLine().toLowerCase();
    		consoleFirstFamilyName = console.nextLine().toLowerCase();
    		consoleSecondFamilyName = console.nextLine().toLowerCase();
    		
    		Student stud1 = new Student(consoleFirstName, consoleFirstFatherName, consoleFirstFamilyName);
    		Student stud2 = new Student(consoleSecondName, consoleSecondFatherName, consoleSecondFamilyName);
    		
    		stud1.compareStudents(stud2);
    		
    	}
    }
    
    class Student {
    	String firstName;
    	String fathersName;
    	String familyName;
    	
    	Student(String firName, String fatName, String famName) {
    		firstName = firName;
    		fathersName = fatName;
    		familyName = famName;
    	}
    	
    	public String getFirstName() {
    		return firstName;
    	}
    
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    
    	public String getFathersName() {
    		return fathersName;
    	}
    
    	public void setFathersName(String fathersName) {
    		this.fathersName = fathersName;
    	}
    
    	public String getFamilyName() {
    		return familyName;
    	}
    
    	public void setFamilyName(String familyName) {
    		this.familyName = familyName;
    	}
    
    	public void compareStudents(Student student2) {
    		if((this.getFirstName()).equals(student2.getFirstName()) && (this.getFathersName()).equals(student2.getFathersName()) && (this.getFamilyName()).equals(student2.getFamilyName()))
    				System.out.println("These are duplicate names");
    		else if((this.getFathersName()).equals(student2.getFathersName()) && (this.getFamilyName()).equals(student2.getFamilyName()))
    			System.out.println("They are brothers or sisters");
    		else if((this.getFamilyName()).equals(student2.getFamilyName()))
    			System.out.println("They are relatives");
    		else
    			System.out.println("May be , they are just friends");
            }
    }
    Last edited by cselic; 04-10-2010 at 10:33 PM.

  5. #5
    marc is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

Posting Permissions

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