1 write a function mysign that will behave like sign inbuilt

1- write a function my_sign that will behave like sign in-built function.

2-write a function precent_even that will take an array of intergers as an input argument, and will return a precentage value of how many integers within the input array are even numbers compared to the total numbers of intergers in the array. you may assume the input array will not be empty

3-write a function approx_alt_harmonic that will receive an integer value of m, the number of terms used n the approximation, and will return the approximation of the sum using m terms.

Solution


import java.util.*;
import java.io.*;
public class cheggclasses {
   public int my_sign(int value){
       if(value==0)
           return 0;
       else if(value>0)
           return 1;
       else
           return -1;
      
   }
   public int my_sign(float value){
       int value1=(int)value;
       if(value1==0)
           return 0;
       else if(value1>0)
           return 1;
       else
           return -1;
      
      
      
   }
   public float precent_even(int[] arr){
       int count=0;
       float remaing=0.0f;
       float percent=0;
       for(int i=0;i<arr.length;i++){
           if(arr[i]%2==0)
               count++;
       }
       remaing=(float)count/arr.length;
       percent=remaing*100;
      
      
       return percent;
   }
   public int aprox_alt_hormonic(int m){
       int sum=0;
       int n=m;
       int temp=0;
       while(n>0){
           temp=n%10;
           sum=sum+temp;
           n=n/10;
          
       }
       return sum;
      
   }
   public float aprox_alt_hormonic(float m){
       int sum=0;
       int temp;
       int toint=0;
       String val=String.valueOf(m);
  
       for(int i=0;i<val.length();i++){
           if(val.charAt(i)!=\'.\'){
           temp=val.charAt(i);
           if(temp==48)
               toint=0;
           else if(temp==49)
               toint=1;
           else   if(temp==50)
               toint=2;
           else   if(temp==51)
               toint=3;
           else   if(temp==52)
               toint=4;
           else   if(temp==53)
               toint=5;
           else if(temp==54)
               toint=6;
           else if(temp==55)
               toint=7;
               else if(temp==56)
               toint=8;
               else   if(temp==57)
                   toint=9;
          
      
          
              
      
      
      
  
           sum=sum+toint;
          
           }
  
       }
       return sum;
   }  
}
mainclass;

public class userdefinedfunctionmain {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int arr[]={222,2,4,32,6,12,14,16,22,21};
       cheggclasses cg=new cheggclasses();
       System.out.println(cg.my_sign(-2));
       System.out.println(cg.my_sign(1.1f));
       System.out.println(\"percentage of even numbers in array is:\"+cg.precent_even(arr));
       System.out.println(\"approximate is:\"+cg.aprox_alt_hormonic(234));
       System.out.println(\"approximate is:\"+cg.aprox_alt_hormonic(1.23f));
   }
}


output:
-1
1
percentage of even numbers in array is:90.0
approximate is:9
approximate is:6.0

1- write a function my_sign that will behave like sign in-built function. 2-write a function precent_even that will take an array of intergers as an input argum
1- write a function my_sign that will behave like sign in-built function. 2-write a function precent_even that will take an array of intergers as an input argum
1- write a function my_sign that will behave like sign in-built function. 2-write a function precent_even that will take an array of intergers as an input argum

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site