Answer to Question #322882 in Java | JSP | JSF for Claire

Question #322882

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

1
Expert's answer
2022-04-03T07:59:38-0400
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();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS