Question

In: Computer Science

How is this method coded in java? int differential(int[] post) differential computes the virtual differential for...

How is this method coded in java?

int differential(int[] post)

differential computes the virtual differential for a single post record and returns the differential. The differential is simply the difference between ups and downs. differential is virtual because it is not stored in a post record.

/// Compute the differential between "ups" (at index 1) and "downs"

  /// (at index 2). The differential is not maintained in the data but is a

  /// virtual field derived by the calculation performed here

  public static int differential(int[] post) {

    // TODO : Implement Here

    return 0;

  }

Post data:

0001 505361 236677

0002 629710 727941

0003 414780 842861

0004 702957 401854

0005 862865 315572

0006 930013 609421

0007 326101 220500

0008 732457 404837

0009 878259 411163

0010 227645 638470

Solutions

Expert Solution

One of the thing you failed to metion that from where these values are coming. I want to ask that if we are reading these values from a file or they are simply stored in the code.

Here I am providing the solution by simply assuming that we are reading thses values from a file.

Also please not that save the text file in same folder where is your java code.

Working: We simply raead each line from a file and pass to the method as an array where it calculates the differential and return in int which will we print on console.

----------------------------------------------------------------------------------------------------------------

Code:

import java.util.*;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

class Main
{
  
   //method to calculate virtual differential
   public static int differential(int[] post) {

//return the difference of ups (at index 1) to downs (at index 2)
return post[1] - post[2];

}
  
//execution of program starts from here
public static void main(String[] args)
{
       String fileName = "data.txt" ; //save this file in same folder where your java file is stored.
      
       try
       {
           Path path = Paths.get(fileName);
           Scanner scanner = new Scanner(path);
           System.out.println("Reading all tweets from file:\n");
          
           //read each number line by line
           while(scanner.hasNextLine()){
              
               //create an array
               int arr[] = new int[3];
              
               //tweet number
               arr[0] = Integer.parseInt(scanner.next());
               arr[1] = Integer.parseInt(scanner.next()); //ups
               arr[2]= Integer.parseInt(scanner.next()); //downs
              
               //pass these values to method as an array
               int count = differential(arr);
              
               //print the result
               System.out.println("Tweet Num: " + arr[0] + " Virtual Differential: " + count);
           }
           scanner.close();
       }
       catch(Exception e)
       {
           System.out.println("Unable to read file");
       }
  
}
}

----------------------------------------------------------------------------------------

Output:


Related Solutions

How is this method coded in Java?
How is this method coded in Java?
1) Consider the following Java method. Which term best describes what this method computes? static int...
1) Consider the following Java method. Which term best describes what this method computes? static int doSomething(int[] a) {     int b = a[0];     for (int c : a) if (b > c) b = c;     return b; } a. average b. maximum c. minimum d. sum e. transpose 2) Consider the following Java program, what starts on line 2? 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4        ...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Write a Java method that takes an input string and computes the income minus the expenses....
Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'. The input string may contain lowercase and uppercase letters, as well as other characters. Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int. Test cases :...
Write a Java method that computes the future investment value at a given interest rate for...
Write a Java method that computes the future investment value at a given interest rate for a specified number of years. Your method should return the future investment value after calculation. The future investment is determined using the formula below:     futureInvestmentValue = investmentAmount * (1+monthlyInterestRate)numberOfYears*12 You can use the following method header: public static double futureInvestmentValue (double investmentAmount, double monthlyInterestRate, int years);
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT