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
Eutectic product in Fe-C system is called A. Pearlite B. Bainite C. Ledeburite D. Spheroidite
zimovet [89]

Answer:

Eutectic product in Fe-C system is called Ledeburite-C.

6 0
3 years ago
The electric motor exerts a torque of 800 N·m on the steel shaft ABCD when it is rotating at a constant speed. Design specificat
kodGreya [7K]

Answer:

d= 4.079m ≈ 4.1m

Explanation:

calculate the shaft diameter from the torque,    \frac{τ}{r} = \frac{T}{J} = \frac{C . ∅}{l}

Where, τ = Torsional stress induced at the outer surface of the shaft (Maximum Shear stress).

r = Radius of the shaft.

T = Twisting Moment or Torque.

J = Polar moment of inertia.

C = Modulus of rigidity for the shaft material.

l = Length of the shaft.

θ = Angle of twist in radians on a length.  

Maximum Torque, ζ= τ ×  \frac{ π}{16} × d³

τ= 60 MPa

ζ= 800 N·m

800 = 60 ×  \frac{ π}{16} × d³

800= 11.78 ×  d³

d³= 800 ÷ 11.78

d³= 67.9

d= \sqrt[3]{} 67.9

d= 4.079m ≈ 4.1m

3 0
3 years ago
Read 2 more answers
What is this spray pattern defect most likely caused by:
DiKsa [7]

Answer:

fluid nozzle that is too large

6 0
2 years ago
Comparación de hipotecas Los Chos
aleksklad [387]

Answer:

I don't understand the language French sorry can't answer

3 0
3 years ago
When two or more simple machines are combined they form
Volgvan
Compound machine is the answer
8 0
3 years ago
Other questions:
  • A storm sewer is carrying snow melt containing 1.2 g/L of sodium chloride into a small stream. The stream has a naturally occurr
    14·1 answer
  • A sheet of steel 4.4 mm thick has nitrogen atmospheres on both sides at 1200°C and is permitted to achieve a steady-state diffus
    10·1 answer
  • During an office party, an office worker claims that a can of cold beer on his table warmed up to 20oC by picking up energy from
    13·1 answer
  • Model the real world idea of an Address. 123 Main Street Apt #2 Small Town, Iowa 55555 USA Write a class called Address, follow
    11·1 answer
  • The hot water needs of an office are met by heating tab water by a heat pump from 16 C to 50 C at an average rate of 0.2 kg/min.
    5·1 answer
  • Burn in hell i watched your stupid video and i still could not get the answer
    14·1 answer
  • Suppose that tank holds 1% liquid water by volume and 99% vapor water by volume at a temperature of 185oC. What is the quality?
    10·1 answer
  • Is it permissible to install recessed fixture directly against wood ceiling joists? Explain why or why not.
    5·1 answer
  • A blue and a yellow cubes are rolled- What is the probability that a yellow cube is a multiple of 3 and the product is 6?
    7·1 answer
  • Technician A says the compressor is the dividing line of the refrigeration system, low- to high-side. Technician B says the expa
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!