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
The town of Mustang, TX is concerned that waste heat discharged from a new up- stream power plant will decimate the minnow popul
vitfil [10]

Answer:

Yes the water will be safe at the point of cooling water discharge

Explanation:

Power losses in plant= 350- 350×0.35=227.5MW

Rate of heat rejection to stream= 0.75× 227.5= 170.625MW

Rate of heat rejection= rate of flow of water× c × ΔT

170625000= 150000000× 4.186 × (Final temperature- 20)

Final temperature= 20.3 ◦C

The final temperature of stream will be 20.3 ◦C. Thechange is very small so the minnows will be able to handle this temperature.

7 0
3 years ago
Engineers need to be open-ended when dealing with their designs. Why?
melomori [17]
I think the answer would be A if its wrong I’m sorry
7 0
3 years ago
As the impurity concentration in solid solution of a metal is increased, the tensile strength:________.a) decreasesb) increasesc
valkas [14]

Answer:

Increases

Explanation:

By inhibiting the motion of dislocations by impurities in a solid solutions, is a strengthening mechanism. In solid solutions it is atomic level strengthening resulting from resistance to dislocation motion. Hence, the strength of the alloys can differ with respect to the precipitate's property. Example, the precipitate is stronger (ability to an obstacle to the dislocation motion) than the matrix and it shows an improvement of strength.

5 0
3 years ago
How does sea navigation work?
ahrayia [7]

Answer:

a clock

Explanation:

you use a clock in water

3 0
2 years ago
Why is it reasonable to say that no system is 100% efficient?​
Virty [35]

Generally, frictional losses are more predominant for the machines being not 100% efficient. This friction leads to the loss of energy in the form of heat, into the surroundings. Some of the supplied energy may be utilised to change the entropy (measure of randomness of the particles) of the system.

5 0
3 years ago
Other questions:
  • The direction of rotation of a dc series motor or a universal motor connected to a dc power source A) depends on the polarities
    15·1 answer
  • A spherical, stainless steel (k 16 W m1 K-1) tank has a wall thickness of 0.2 cm and an inside diameter of 10 cm. The inside sur
    12·1 answer
  • Two AAA-size lithium batteries are connected in series in a flashlight. Each battery has 3.5 volt and 4- Amp-hour capacity. If t
    8·1 answer
  • what is the expected life 1 inch diameter bar machined from AISI 1020 CD Steel is subjected to alternating bending stress betwee
    9·1 answer
  • The air loss rate for a straight truck or bus with the engine off and the brakes
    11·1 answer
  • Determine the magnitude and the location of the hydrostatic force on the 2m by 4 m vertical rectangular gate shown in Figure P3.
    12·1 answer
  • A misfire code is a type ____ DTC<br> A) 1 or 2<br> B) a or b<br> C) c or d<br> D l or ll
    15·1 answer
  • Please help been stuck on this for a couple minutes
    5·1 answer
  • Select the correct answer <br><br> What is the simplest definition of a manufacturing process?
    5·2 answers
  • What do u mean by double entry bookkeeping system?<br>u fellas don't spam pls​
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!