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
Underground water is to be pumped by a 78% efficient 5- kW submerged pump to a pool whose free surface is 30 m above the undergr
maksim [4K]

Answer:

a) The maximum flowrate of the pump is approximately 13,305.22 cm³/s

b) The pressure difference across the pump is approximately 293.118 kPa

Explanation:

The efficiency of the pump = 78%

The power of the pump = 5 -kW

The height of the pool above the underground water, h = 30 m

The diameter of the pipe on the intake side = 7 cm

The diameter of the pipe on the discharge side = 5 cm

a) The maximum flowrate of the pump is given as follows;

P = \dfrac{Q \cdot \rho \cdot g\cdot h}{\eta_t}

Where;

P = The power of the pump

Q = The flowrate of the pump

ρ = The density of the fluid = 997 kg/m³

h = The head of the pump = 30 m

g = The acceleration due to gravity ≈ 9.8 m/s²

\eta_t = The efficiency of the pump = 78%

\therefore Q_{max} = \dfrac{P \cdot \eta_t}{\rho \cdot g\cdot h}

Q_{max} = 5,000 × 0.78/(997 × 9.8 × 30) ≈ 0.0133 m³/s

The maximum flowrate of the pump Q_{max} ≈ 0.013305 m³/s = 13,305.22 cm³/s

b) The pressure difference across the pump, ΔP = ρ·g·h

∴ ΔP = 997 kg/m³ × 9.8 m/s² × 30 m = 293.118 kPa

The pressure difference across the pump, ΔP ≈ 293.118 kPa

6 0
3 years ago
What is acid mine drainage
avanturin [10]
Acid mine drainage is the formation and movement of highly acidic water rich in heavy metals. This acidic water forms through the chemical reaction of surface water (rainwater, snowmelt, pond water) and shallow subsurface water with rocks that contain sulfur-bearing minerals, resulting in sulfuric acid.
7 0
3 years ago
Which of the following refers to a full-scale version of a product used to validate performance?
kompoz [17]
I’m thinking it would be c sorry if it’s wrong .
4 0
3 years ago
Read 2 more answers
A cylindrical 1045 steel bar is subjected to repeated compression-tension stress cycling along its axis. If the load amplitude i
Gre4nikov [31]

Answer:

13.4 mm

Explanation:

Given data :

Load amplitude ( F )  = 22,000 N

factor of safety ( N )= 2.0

Take ( Fatigue limit stress amplitude for this alloy ) б = 310 MPa

<u>calculate the minimum allowable bar diameter to ensure that fatigue failure will not occur</u>

minimum allowable bar diameter = 13.4 * 10^-3 m ≈ 13.4 mm

<em>attached below is a detailed solution</em>

3 0
3 years ago
What does STP and NTP stands for in temperature measurement?
Lisa [10]

STP stands for standard temperature pressure and NTP stands for normal temperature pressure

8 0
4 years ago
Other questions:
  • How does running an electric current through wire cause a magnetic field?
    6·1 answer
  • ). A company periodically tests its product for tread wear under simulated conditions. Thirty random samples, each containing 5
    11·1 answer
  • What is the difference between tension and compression?
    9·1 answer
  • Explain the use of remote sensing in surveying.​
    8·1 answer
  • What is wrong with the following code?<br> 6<br> print (what is your name?)
    9·2 answers
  • According to the video, what are some tasks that Construction Managers perform? Check all that apply.
    9·2 answers
  • For a nozzle-duct system shown in Fig Q3, the nozzle is designed to produce a Mach number of 2.8 with y = 1.4, The inlet conditi
    14·1 answer
  • NO SCAMS
    9·2 answers
  • Which type of Artificial Intelligence (AI) can repeatedly perform tasks of limited scope?
    12·1 answer
  • How many and what type of<br> receptacles are connected to<br> this circuit?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!