1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pickupchik [31]
3 years ago
6

IN JAVA,

Engineering
1 answer:
Citrus2011 [14]3 years ago
8 0

Answer:

Explanation:

Code:

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class Knapsack {

 

  public static void knapsack(int wk[], int pr[], int W, String ofile) throws IOException

  {

      int i, w;

      int[][] Ksack = new int[wk.length + 1][W + 1];

     

      for (i = 0; i <= wk.length; i++) {

  for (w = 0; w <= W; w++) {

  if (i == 0 || w == 0)

  Ksack[i][w] = 0;

  else if (wk[i - 1] <= w)

  Ksack[i][w] = Math.max(pr[i - 1] + Ksack[i - 1][w - wk[i - 1]], Ksack[i - 1][w]);

  else

  Ksack[i][w] = Ksack[i - 1][w];

  }

  }

     

      int maxProfit = Ksack[wk.length][W];

      int tempProfit = maxProfit;

      int count = 0;

      w = W;

      int[] projectIncluded = new int[1000];

      for (i = wk.length; i > 0 && tempProfit > 0; i--) {

         

      if (tempProfit == Ksack[i - 1][w])

      continue;    

      else {

          projectIncluded[count++] = i-1;

      tempProfit = tempProfit - pr[i - 1];

      w = w - wk[i - 1];

      }

     

      FileWriter f =new FileWriter("C:\\Users\\gshubhita\\Desktop\\"+ ofile);

      f.write("Number of projects available: "+ wk.length+ "\r\n");

      f.write("Available employee work weeks: "+ W + "\r\n");

      f.write("Number of projects chosen: "+ count + "\r\n");

      f.write("Total profit: "+ maxProfit + "\r\n");

     

  for (int j = 0; j < count; j++)

  f.write("\nProject"+ projectIncluded[j] +" " +wk[projectIncluded[j]]+ " "+ pr[projectIncluded[j]] + "\r\n");

  f.close();

      }    

  }

 

  public static void main(String[] args) throws Exception

  {

      Scanner sc = new Scanner(System.in);

      System.out.print("Enter the number of available employee work weeks: ");

      int avbWeeks = sc.nextInt();

      System.out.print("Enter the name of input file: ");

  String inputFile = sc.next();

      System.out.print("Enter the name of output file: ");

      String outputFile = sc.next();

      System.out.print("Number of projects = ");

      int projects = sc.nextInt();

      int[] workWeeks = new int[projects];

      int[] profit = new int[projects];

     

      File file = new File("C:\\Users\\gshubhita\\Desktop\\" + inputFile);

  Scanner fl = new Scanner(file);

 

  int count = 0;

  while (fl.hasNextLine()){

  String line = fl.nextLine();

  String[] x = line.split(" ");

  workWeeks[count] = Integer.parseInt(x[1]);

  profit[count] = Integer.parseInt(x[2]);

  count++;

  }

 

  knapsack(workWeeks, profit, avbWeeks, outputFile);

  }

}

Console Output:

Enter the number of available employee work weeks: 10

Enter the name of input file: input.txt

Enter the name of output file: output.txt

Number of projects = 4

Output.txt:

Number of projects available: 4

Available employee work weeks: 10

Number of projects chosen: 2

Total profit: 46

Project2 4 16

Project0 6 30

You might be interested in
QUESTION:
svet-max [94.6K]
OA bloom is smaller than a bar
6 0
3 years ago
The inverted U-tube is used to measure the pressure difference between two points A and B in an inclined pipeline through which
JulijaS [17]

Answer:

i) 0.610 m or 610 mm

ii) 0.4 m or 400 mm

Explanation:

The pressure difference between the pipes is

a) Air

Pa + πha +Ha = Pb + πhb +Hb

Pa - Pb = π(hb-ha) + Hb-Ha

Relative density of air = 1.2754 kg /m3

Pa - Pb = 1.2754 * 0.4 + (0.3-0.2) = 0.610 m or 610 mm

b) paraffin of relative density of 0.75

Pa - Pb = π(hb-ha) + Hb-Ha

Pa - Pb = 0.75 * 0.4 + (0.3-0.2) = 0.4 m or 400 mm

8 0
3 years ago
Steam enters an adiabatic turbine at 7 MPa, 600°C, and 80 m/s and leaves at 50 kPa, 150°C, and 140 m/s. If the power output of t
katrin2010 [14]

Answer:

mass flow=6.84kg/s

efficiency=73.5%

Explanation:

Hi!

To solve this problem follow the steps below, the procedure is attached in an image

1. Draw the complete outline of the problem.

2.Through laboratory tests, thermodynamic tables were developed, these allow to know all the thermodynamic properties of a substance (entropy, enthalpy, pressure, specific volume, internal energy etc ..)  

through prior knowledge of two other properties such as pressure and temperature.  

use thermodynamic tables to determine the following thermodynamic properties:

a. enthalpy at the turbine inlet

b. entropy at the turbine entrance

c. ideal enthalpy at the turbine outlet

d. real enthalpy at the turbine outlet

3.  use the turbine efficiency equation and use the enthalpies found in step 2

4.

uses the first law of thermodynamics that states that the energy that enters a system is the same that must come out, in this way we find the mass flow

6 0
3 years ago
Assume you have four fins, each with a mass of 8.0 grams. What is the total weight of these four fins? (Hint: watch your units!)
soldier1979 [14.2K]
The answer is 32.0 grams
7 0
3 years ago
Tech A says that serviceable wheel bearings can be repacked by removing the dust cap, filling it with grease, and reinstalling i
rewona [7]

Serviceable wheel bearings can be repacked by removing the dust cap and the cotter pin must be replaced with a new one every time it is removed. Therefore, both Tech A and B are correct.

Sealed bearing assemblies are typically prefilled with lubricant. They also make a more reliable and consistent installation process due to the fact that every into comes preset at the proper clearance.

Furthermore, the serviceable wheel bearings can be repacked by removing the dust cap, filling it with grease, and reinstalling it. Also, cotter pin must be replaced with a new one every time it is removed.

Therefore, both tech A and B are correct.

Read related link on:

brainly.com/question/16612086

8 0
2 years ago
Other questions:
  • The natural water content of a Boston Blue Clay soil sample was found to be 14.6% through oven drying. It has been calculated th
    8·1 answer
  • Air enters a compressor operating at steady state at 1.05 bar, 300 K, with a volumetric flow rate of "84" m3/min and exits at 12
    7·1 answer
  • (30 pts) A simply supported beam with a span L=20 ft and cross sectional dimensions: b=14 in; h=20 in; d=17.5 in. is reinforced
    13·1 answer
  • If you make a mistake in polarity when measuring the value of DC voltage in a circuit with a digital VOM, what will happen? A. T
    14·1 answer
  • Determine if the fluid is satisfied​
    10·1 answer
  • Question in image. Question from OSHA.
    11·2 answers
  • . Find the derivative of y = x² – 5x using the definition of the derivative.
    6·1 answer
  • Pressure sensor rated at 0-500 psi and has an output of 0-10 Volts DC (10 V correspond to
    15·1 answer
  • If a client is shown the "Cadillac"
    11·1 answer
  • A thermodynamicist claims to have developed a heat pump with a COP of 1.7 when operating with thermal energy reservoirs at 273 K
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!