Write a program that will input the names, ages and weights of three siblings and display the lightest followed by the youngest of the siblings. Do the above assignment without using an array of objects. Implementation Create a project asSibling. In the above project, create a class Sibling not containing the main method. In the above project, create a class TestSibling containing the main method.

Respuesta :

Answer:

Here is the JAVA program:

Sibling.java class:

import java.util.Scanner;  //to accept input from user

public class Sibling {  //class name

//private data members of class

   private String name;  // String type variable to hold the name of sibling

   private int age;  // int type variable to hold the age of sibling

   private int weight;  // int type variable to hold the weight of sibling

   public Sibling() {}     //constructor of class Sibling

   public Sibling (String n, int a, int w)  {  //parameterized constructor

       name = n;  // refers to name field

       age = a;  // refers to age field

       weight = w;    }   // refers to weight field

   public String getName ()    {   //accessor method to get the current name of sibling

       return name;    }   //returns the name field of the sibling

   public int getAge ()    {   //accessor method to get the current age of sibling

       return age;    }   //returns the age field of the sibling

   public int getWeight (){ //accessor method to get the current weight of sibling

    return weight;    } //returns the weight field of the sibling

   public void getInput() {    //to take input name,age and weight from user

 Scanner input = new Scanner(System.in);  //Scanner class object

 System.out.print("Enter the name:  ");  //prompts user to enter the name of sibling

 name = input.nextLine();  //scans and reads the input name from user

 System.out.print("Enter the age:  ");  //prompts user to enter the age of sibling

 age = input.nextInt();   //scans and reads the age from user

 System.out.print("Enter the weight:  ");  //prompts user to enter the weight of sibling

 weight = input.nextInt(); } }   //scans and reads the input weight from user

Explanation:

Here is the TestSibling class that contains main method:

public class TestSibling {   //class name

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

String name;  // to hold name of sibling

int age, weight;   // to hold age and weight of sibling

Sibling sib1, sib2, sib3;   // objects of Sibling class

sib1 = new Sibling ();  // creates object of class Sibling for sibling 1 and calls constructor

sib1.getInput();   // calls getInput method using sib1 object to get the name, age and weight of sibling 1

sib2 = new Sibling ();  // creates object of class Sibling for sibling 2 and calls constructor

sib2.getInput();   //calls getInput method using sib2 object to get the name, age and weight of sibling 2

sib3 = new Sibling ();  //creates object of class Sibling for sibling 3 and calls constructor

sib3.getInput();   //calls getInput method using sib3 object to get the name, age and weight of sibling 3

Sibling youngest=null, lightest=null;    //holds the youngest age and lightest weight of siblings

if (sib1.getAge( )<= sib2.getAge( ) && sib1.getAge( ) <= sib3.getAge( ) )   /*if condition checks if age of sibling 1 is less than or equals to that of sibling 2 and sibling 3,  using object of each sibling and getAge method to access age. the logical operator && AND is used so that if condition evaluates to true if sib1 is younger than both sib2 and sib3 */

{ youngest=sib1;}   //if the above condition is true then sets sib1 as youngest

else if (sib2.getAge( ) <= sib1.getAge( ) && sib2.getAge( ) <= sib3.getAge( ) )   // else if condition checks if age of sibling 2 is less than or equals to that of sibling 1 and sibling 3

{youngest=sib2;}   // if above condition is true then sets sib2 as the youngest

else   //if none of the above condition is true then this means that the third has the lowest age

{            youngest=sib3;    }    //sets sib3 as the youngest

if (sib1.getWeight( ) <= sib2.getWeight( ) && sib1.getWeight( ) <= sib3.getWeight( ) )   //if condition checks if weight of sibling 1 is less than or equals to that of sibling 2 and sibling 3,  using object of each sibling and getAge method to access weight of each sibling

          { lightest=sib1; }   //if the above condition is true then sets sib1 as having the lightest weight

else if (sib2.getWeight( ) <= sib1.getWeight( ) && sib2.getWeight( ) <= sib3.getWeight( ) )  // else if condition checks if weight of sibling 2 is less than or equals to that of sibling 1 and sibling 3

{lightest=sib2; }  //if the above condition is true then sets sib2 as the lightest

else  //if none of the above condition is true then this means that the third has the lightest weight

{ lightest=sib3;   }  //sets sib3 as the lightest

System.out.println("The lightest sibling: " + lightest.getName() +" " + lightest.getAge()+" "+ lightest.getWeight());  } } //calls the getName() getAge() and getWeight() method using object lightest to print the lightest of the siblings

System.out.println("The youngest sibling: " + youngest.getName() +" " + youngest.getAge()+" "+ youngest.getWeight());  //calls the getName() getAge() and getWeight() method using object youngest to print the youngest of the siblings

The screenshot of the output is attached.

Ver imagen mahamnasir
RELAXING NOICE
Relax