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
Which of the following is used in the electrical field?
weeeeeb [17]

Answer:

pliers

Explanation:

because that makes the most sense

6 0
3 years ago
Q.17) A 50-acre catchment containing cropland is converted ot a Qatar mail
Ghella [55]

Answer:

Option D

Explanation:

A post development hydrograph will have lower concentration time and lower infiltration losses and hence sooner peak and higher peak and more runoff or higher area under graph. Therefore, all the answers are correct hence option D

3 0
2 years ago
A coil with an average diameter of 5 inch will have an area of ""blank"" square meters
nadezda [96]

Answer:

19.64 square inches

Explanation:

Area will be (¶d^2)/4

= (3.142 x 5^2)/4

= 19.64 square inches

8 0
3 years ago
Which of the following statements about resistance is TRUE?
valentina_108 [34]

Answer:

I think it's the no 3rd

Explanation:

hope this helps

3 0
2 years ago
An amplifier which needs a high input resistance and a high output resistance is : Select one: a. A voltage amplifier b. None of
True [87]

Answer:

None of these

Explanation:

There are different types of amplifiers, and each has different characteristics.

  • Voltage amplifier needs high input and low output  resistance.
  • Current amplifier needs Low Input and High Output  resistance.
  • Trans-conductance amplifier Low Input and High Output resistance.
  • Trans-Resistance amplifier requires High Input and Low output  resistance.

Therefore, the correct answer is "None of these "

3 0
2 years ago
Other questions:
  • What is a construction worker with limited skills called?
    12·1 answer
  • Define various optical properties of engineering materials
    11·1 answer
  • A closed, rigid, 0.45 m^3 tank is filled with 12 kg of water. The initial pressure is p1 = 20 bar. The water is cooled until the
    15·1 answer
  • The viscosity of the water was 2.3×10^−5lb⋅⋅s/ft^2 and the water density was 1.94 slugs/ft^3. Estimate the drag on an 88-ft diam
    13·1 answer
  • The heat input to an Otto cycle is 1000kJ/kg. The compression ratio is 8 and the pressure and temperature at the beginning of th
    8·1 answer
  • If a signal is transmitted at a power of 250 mWatts (mW) and the noise in the channel is 10 uWatts (uW), if the signal BW is 20M
    13·1 answer
  • Consider a section of muscle tissue of a cylindrical shape with a radius of 1.5 cm. During highly rigorous exercise, metabolic p
    8·1 answer
  • If you are setting up a race car. What is the cross weight? Does it matter?
    5·1 answer
  • Match the scenario to the problem-solving step it represents.
    7·1 answer
  • The notation on one's license that the person must wear glasses
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!