Question

In: Computer Science

please write a java code, one for method and another for the Demo to: -Compute the...

please write a java code, one for method and another for the Demo to:

-Compute the average age of all female students.

-Compute the least amount of credits completed among males.

Store the three arrays in the demo file.

Print the results within the demo file.

String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"};

int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21, 21, 21, 23, 20, 20, 19, 18, 19, 21, 20, 22};

int []credits_completed = {45, 12, 54, 23, 26, 33,18, 16, 28, 34, 36, 33, 49, 58, 51, 40, 38, 48, 56, 10}

Solutions

Expert Solution

PROGRAM :

I have run and compiled the program using Eclipse IDE . I am attaching the code along with the input and output screenshot.

PROGRAM CODE


import java.util.Scanner;

public class Demo 
{

        public static void main(String[] args) 
        {
                // TODO Auto-generated method stub

                Scanner sc = new Scanner(System.in);
                String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"};
                int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21, 21, 21, 23, 20, 20, 19, 18, 19, 21, 20, 22};
                int []credits_completed = {45, 12, 54, 23, 26, 33,18, 16, 28, 34, 36, 33, 49, 58, 51, 40, 38, 48, 56, 10};

                //function is being called here.
                averageAge(gender,age,credits_completed);
        }

        private static void averageAge(String[] gender, int[] age, int[] credits_completed) 
        {
                // TODO Auto-generated method stub
                // Here we are calculating the average age of all female students.
                int sum=0 ,count=0;
                for(int i=0;i<gender.length;i++)
                {
                        //checking the condition of geneder is Female or not.
                        if(gender[i].equals("F"))
                        {
                                sum = sum+age[i];
                                count++;
                        }
                }
                float averageAge_female;
                averageAge_female=sum/count;
                System.out.println("THE AVERAGE AGE OF ALL FEMALE STUDENTS IS :"+averageAge_female);
                
                //Here we are calculating the least credits completed by male.
                int flag = 0;
                int least = 0;
                for(int i=0;i<gender.length;i++)
                {
                        if(flag==0 && gender[i].equals("M"))
                        {
                                flag = 1;
                                least = credits_completed[i];
                        }
                        else if(gender[i].equals("M") && least>credits_completed[i])
                        {
                                least = credits_completed[i];
                        }
                }
                System.out.println("THE LEAST AMOUNT OF CREDITS COMPLETED AMONG MALE IS : "+least);
        }

}

PROGRAM INPUT SNAPS

PROGRAM OUTPUT SNAPS

I have made seperately two classes one for printing the value only and other for calculation.

CODES ARE FOLLOWING :

package Demo;

import java.util.Scanner;

import student.Student;

public class DemoMain 
{

        public static void main(String[] args) 
        {
                // TODO Auto-generated method stub
                Scanner sc = new Scanner(System.in);
                String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"};
                int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21, 21, 21, 23, 20, 20, 19, 18, 19, 21, 20, 22};
                int []credits_completed = {45, 12, 54, 23, 26, 33,18, 16, 28, 34, 36, 33, 49, 58, 51, 40, 38, 48, 56, 10};

                DemoMethod d = new DemoMethod();

                //function is being called here for calculating the average of females.
                float avg = d.averageAge(gender,age,credits_completed);
                System.out.println("THE AVERAGE AGE OF ALL FEMALE STUDENTS IS :"+avg);
                
                //functions is being called for finding the least.
                int leastvalue = d.leastValue(gender,age,credits_completed);
                System.out.println("THE LEAST AMOUNT OF CREDITS COMPLETED AMONG MALE IS : "+leastvalue);

        }

}
package Demo;

public class DemoMethod 
{

        // Here we are calculating the average age of all female students.
        public float averageAge(String[] gender, int[] age, int[] credits_completed) 
        {
                // TODO Auto-generated method stub
                int sum=0 ,count=0;
                for(int i=0;i<gender.length;i++)
                {
                        //checking the condition of geneder is Female or not.
                        if(gender[i].equals("F"))
                        {
                                sum = sum+age[i];
                                count++;
                        }
                }
                float averageAge_female;
                averageAge_female=sum/count;
                return averageAge_female;       
        }

        //Here we are calculating the least credits completed by male.
        public int leastValue(String[] gender, int[] age, int[] credits_completed) {
                // TODO Auto-generated method stub
                int flag = 0;
                int least = 0;
                for(int i=0;i<gender.length;i++)
                {
                        if(flag==0 && gender[i].equals("M"))
                        {
                                flag = 1;
                                least = credits_completed[i];
                        }
                        else if(gender[i].equals("M") && least>credits_completed[i])
                        {
                                least = credits_completed[i];
                        }
                }
                return least;
        }
        
}

ATTACHING THE SCREENSHOT

OUTPUT


Related Solutions

write a java code, please do not use method and demo Consider a four digit number...
write a java code, please do not use method and demo Consider a four digit number such as 6587. Split it at two digits, as 65 and 87. Sum of 65 and 87 is 152. Sum of the digits of 152 is 8. Given the starting and ending four digit numbers and a given target number such as 10, write a program to compute and print the number of instances where the sum of digits equals the target.
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy...
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy your Bisect code and modify).   Evaluation of F(x) and F'(x) should be done in a subprogram FCN(x).   The code should ask for input of: x0, TOL, maxIT (and should print output similar to Bisect code).   Debug on a simple problem, like x2−3 = 0.   Then use it to find root of F(x) in [1,2] with TOL=1.e-12. Now consider the problem of finding zeros of      ...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
JAVA CODE, USE COMMENTS TO EXPLAIN PLEASE Write a Bottle class. The Bottle will have one...
JAVA CODE, USE COMMENTS TO EXPLAIN PLEASE Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString() method will be...
Write this code in java and don't forget to comment every step. Write a method which...
Write this code in java and don't forget to comment every step. Write a method which asks a baker how hot their water is, and prints out whether it is OK to make bread with the water. If the water is at or above 110F, your method should print "Too Warm." If the water is below 90.5F, print "Too Cold." If it is in between, print "Just right to bake!" For example, if the user inputs the number 100.5, the...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT