## Amdocs Interview Question

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````/*
Define a class Passenger having the fields name (String), age (int), gender (char : M/F), weight (int). Define 2 constructors for this class, one with parameters and one which has no parameters.
Also define a class Train in which there are the following functions. Note that there are no fields in the class Train.
- int getTotalWeight(Passenger[] passengers)  - returns the total weight of all the passengers in the input
- int countChildren(Passenger[] passengers)  - returns the number of children in the input passengers. A child is defined as age 12 or below
- int countGender(Passenger[] passengers, boolean male)  - returns the number of males if true else returns the number of females. A male has the gender 'M' and a female 'F'
- Passenger[] addPassenger(Passenger[] passengers, Passenger passenger1) - returns a Passenger[] which contains all the passengers and also passenger1.
*/
class Passenger{
String name;
int age, weight;
char gender;
//default constructor i.e constructor without argument.
Passenger(){}

Passenger(String name, int age, char gender, int weight)
{
this.name=name;
this.gender=gender;
this.age=age;
this.weight=weight;
}
}
class Train{
int getTotalWeight(Passenger[] passengers)
{
int totalPassengersWeight=0;
for(int i=0; i<passengers.length; i++)
{
totalPassengersWeight+=passengers[i].weight;
}
}

public int countChildren(Passenger[] passengers)
{
int totalNumOfChild=0;
for(int i=0; i<passengers.length; i++)
{
if(passengers[i].age<=12)
{
totalNumOfChild++;
}
}
}

public int countGender(Passenger[] passengers,  boolean male)
{
int numOfPerson=0;
if(male==true)
{
for(int i=0; i<passengers.length; i++)
{
if(passengers[i].gender=='M')
{
numOfPerson++;
}
}
}
else
{
for(int i=0; i<passengers.length; i++)
{
if(passengers[i].gender=='F')
{
numOfPerson++;
}
}
}
return numOfPerson;
}
{
Passenger[] newPassenger=new Passenger[passengers.length+1];
// copy previous array and also insert new 	passenger1 to new array.
for(int i=0;i<newPassenger.length;i++)
{
if(i<passengers.length){
newPassenger[i]=passengers[i];
}
else
{
newPassenger[i]=passenger1;
}
}
return newPassenger;
}
}

public class TestAfter4a { // main method
public static void main(String args[])
{

// calling or invoke default constructor i.e Passenger()
Passenger obj=new Passenger(); // assign default value to field.
System.out.println("\n\n======calling the default constructor i.e without parameter==== " );
System.out.println("Name="+obj.name +"\tAge=" +obj.age +"\tGender=" +obj.gender +"\tWeight=" +obj.weight);
System.out.println("\n\n======calling the  constructor which have  parameters or arguments==== " );
Passenger passenger1=new Passenger("Alok kumar",23,'M',55);
Passenger passenger2=new Passenger("Amar kumar",10,'M',30);
Passenger passenger3=new Passenger("Cheeku",23,'M',60);
Passenger passenger4=new Passenger("Neha Singh",20,'F',45);
Passenger passenger5=new Passenger("Shruti Haashan",30,'F',56);
Passenger passenger6=new Passenger("Aman raj",11,'M',36);
Passenger passenger7=new Passenger("Amrita kaur",28,'F',54);
Passenger[] passengers={passenger1,passenger2,passenger3,passenger4,passenger5,passenger6,passenger7};

Train objTrain=new Train(); // object of Train class is created for accessing member or method.
System.out.println("Total weight of Passengers is:"+objTrain.getTotalWeight( passengers));
System.out.println("There are total "+objTrain.countChildren(passengers)+" Children passengers.");
System.out.println("There are total "+objTrain.countGender(passengers, true)+ " Male passengers.");
System.out.println("There are total "+objTrain.countGender(passengers, false)+" Female passengers.");
System.out.println("Before adding there are "+passengers.length+" passengers in the train.");
Passenger passenger8=new Passenger("Pinki arora",27,'F',49);
Passenger passenger9=new Passenger("rahul Rock",22,'M',50);
System.out.println("After adding there are "+passengers.length+" passengers in the train.");
System.out.println("Again After adding there are "+passengers.length+" passengers in the train.");
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

class Passenger {
String name;
int age;
char gender;
int weight;
public Passenger(){
name=null;
age=0;
gender='\u0000';
weight=0;
}
public Passenger(String name,int age,char gender,int weight){
this.name=name;
this.age=age;
this.gender=gender;
this.weight=weight;
}
}
public class Train {
public int getTotalWeight(Passenger[] passengers){
int totalWeight=0;
for (int i=0;i< passengers.length;i++){
totalWeight=totalWeight+passengers[i].weight;
}
}
public int countChildren(Passenger[] passengers){
int count=0;
for (int i=0;i< passengers.length;i++){
if(passengers[i].age<=12){
count=count+1;
}
}
return count;
}
public int countGender(Passenger[] passengers,boolean male){
int countMale=0;
int countFemale=0;
for (int i=0;i< passengers.length;i++){
if(passengers[i].gender=='M'){
countMale=countMale+1;
}
else{
countFemale=countFemale+1;
}
}
if (male==true){
return countMale;
}
else{
return countFemale;
}
}
Passenger[] newPassengers= new Passenger[passengers.length+1];
for(int i=0;i<newPassengers.length;i++){
if (i<passengers.length){
newPassengers[i]=passengers[i];
}
else{
newPassengers[i]=passenger1;
}
}
return newPassengers;
}
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.