Menu
Question Index
...

What will be the output of the following program?

public class MarriagesAreMadeInHeaven {
    public static void main(String[] args) {
        Person rani = new Person("Rani", true, 5, 0, 14, true);
        Person raja = new Person("Raja", false, 5, 3, 15, true);
        Person rajesh = new Person("Rajesh", false, 10, 10, 3, false);
        Person rachana = new Person("Rachana", true, 10, 10, 3, false);
        printCompatibility(raja, rani); printCompatibility(raja, rajesh);
        printCompatibility(rachana, raja); printCompatibility(rani, rajesh);
        printCompatibility(rani, rachana);
    }
    private static void printCompatibility(Person person, Person other) {
        System.out.print("[" + person.name + "_" + other.name + "=" + person.compatibility(other) + "]");
    }
}
class Person {
    String name; boolean female, morningPerson; int spendingIndex, badHabitsIndex, philanthropyIndex;
    public Person(String name, boolean female, int spendingIndex, int badHabitsIndex, int philanthropyIndex, boolean morningPerson) {
        this.name = name; this.female = female; this.morningPerson = morningPerson;
        this.spendingIndex = spendingIndex;
        this.badHabitsIndex = spendingIndex;
        this.philanthropyIndex = philanthropyIndex;
    }
    public int compatibility(Person other) {
        int result = -1;
        if (female != other.female) {
            result += 10 - Math.abs(spendingIndex - other.spendingIndex);
            result += 5 - Math.abs(badHabitsIndex - other.badHabitsIndex);
            result += 20 - Math.abs(philanthropyIndex - other.philanthropyIndex);
            if (morningPerson == other.morningPerson) result += 4;
        }
        return result;
    }
}


[Raja_Rani=29][Raja_Rajesh=-1][Rachana_Raja=5][Rani_Rajesh=3][Rani_Rachana=-1]
[Raja_Rani=37][Raja_Rajesh=-1][Rachana_Raja=12][Rani_Rajesh=13][Rani_Rachana=-1]
[Raja_Rani=34][Raja_Rajesh=-1][Rachana_Raja=10][Rani_Rajesh=8][Rani_Rachana=-1]
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Advanced Java Programming Concepts

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App