Copy the code to an IDE and do the required changes:
import java.util.*; public class sometp4 { static float optimalBST(String[] words, float[] freq, int n) { //2D dp matrix float[][] dp = new float[n + 1][n + 1]; int[][] root = new int[n + 1][n + 1]; // For a single word, cost is equal to frequency of the word for (int i = 0; i < n; i++) dp[i][i] = freq[i]; // Now consider for size 2, 3, ... . for (int L = 2; L <= n; L++) { for (int i = 0; i <= n - L + 1; i++) { // Get column number j from row number i and size L int j = i + L - 1; dp[i][j] = Integer.MAX_VALUE; for (int r = i; r <= j; r++) { // c = cost when words[r] becomes root of this subtree float c = ((r > i) ? dp[i][r - 1] : 0) + ((r < j) ? dp[r + 1][j] : 0) + sum(freq, i, j); if (c < dp[i][j]) { dp[i][j] = c; root[i][j] = r; } } } } // Print computed matrices System.out.println("*******Cost matrices******\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(dp[i][j] + " "); } System.out.print("\n"); } //print the root matrices System.out.println("\n\n******Root matrices*****\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(root[i][j] + " "); } System.out.print("\n"); } //return the optimal cost return dp[0][n - 1]; } // function to get sum of freq[i] to freq[j] static float sum(float[] freq, int i, int j) { float s = 0; for (int k = i; k <= j; k++) { if (k >= freq.length) continue; s += freq[k]; } return s; } public static void main(String[] args) throws Exception{ //Suppose your name is 'someone sometitle' sometp4 obst = new sometp4(); System.out.println("\n\tProgram to compute average number of comparisons for an" + "\n\tOptimal Binary Search Tree (obst) using dynamic programming." + "\n\tPrepared By: sometitle, someone" + "\n\tStudent ID: x123456 Date: 01\\01\\1970"); obst.readinput(); obst.compute(); obst.printmatrices(); obst.printree(); //Perform the below tasks in desired methods created below Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] words = new String[n]; float[] freq = new float[n]; for (int i = 0; i < n; i++) { words[i] = sc.next(); freq[i] = sc.nextFloat(); } System.out.println("\ncost is " + optimalBST(words, freq, n)); sc.close(); } private void printree() { } private void printmatrices() { } private void compute() { } private void readinput() { } }
need help to write the main method as the template import java.util.*; public class oBST {...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...
Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
I need help running this code. import java.util.*; import java.io.*; public class wordcount2 { public static void main(String[] args) { if (args.length !=1) { System.out.println( "Usage: java wordcount2 fullfilename"); System.exit(1); } String filename = args[0]; // Create a tree map to hold words as key and count as value Map<String, Integer> treeMap = new TreeMap<String, Integer>(); try { @SuppressWarnings("resource") Scanner input = new Scanner(new File(filename));...
Need help with the UML for this code? Thank you. import java.util.Scanner; public class Assignment1Duong1895 { public static void header() { System.out.println("\tWelcome to St. Joseph's College"); } public static void main(String[] args) { Scanner input = new Scanner(System.in); int d; header(); System.out.println("Enter number of items to process"); d = input.nextInt(); ...
import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read in the value of k int k = Integer.parseInt(sc.nextLine()); // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } } System.out.println(findPairs(numbers, k)); } //method that...
Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }
Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn); else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...
Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int [][]courses=new int [10][2]; for(int i=0;i<10;i++) { while(true) { System.out.print("Enter classes and graduation year for student "+(i+1)+":"); courses[i][0]=sc.nextInt(); courses[i][1]=sc.nextInt(); if(courses[i][0]>=1...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...