Create objec-oriented java program that will ask the user for 2 names ( boy and girl).
1. Two (2) classes -MyMainClass and Other Class
2. One (1) Constructor - set the name of the girl and boy (for object)
3. Two (2) accessors
getBoyNumLetters () & getGirlNameLetters (return numbers of letters)
4. One (1) mutator - check (to check who has the higher number of letters)
Sample output:
Enter the name of the girl: Claire
Enter the name of a boy: Christian
Claire has 6 letters of her name.
Christian has 9 letters in his name
The boy's name has more letters than the girls
import java.util.Scanner;
class OtherClass{
String nameW, nameM;
OtherClass(String nameW, String nameM) {
this.nameW = nameW;
this.nameM = nameM;
}
public int getBoyNumLetters(){
return nameM.length();
}
public int getGirlNameLetters(){
return nameW.length();
}
public void check(){
System.out.printf("%s has %d letters of her name.", nameW, getGirlNameLetters());
System.out.println("");
System.out.printf("%s has %d letters of her name.", nameM, getBoyNumLetters());
System.out.println("");
if(getBoyNumLetters() > getGirlNameLetters()){
System.out.println("The boy's name has more letters than the girls");
} else {
System.out.println("The girl's name has more letters than the boy");
}
}
}
public class MyMainClass {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the name of the girl: ");
String nameGirl = in.nextLine();
System.out.print("Enter the name of the boy: ");
String nameBoy = in.nextLine();
OtherClass othCla = new OtherClass(nameGirl, nameBoy);
othCla.getGirlNameLetters();
othCla.getBoyNumLetters();
othCla.check();
}
}
Comments
Leave a comment