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 solid rod BC has a diameter of 30 mm and is made of an aluminum for which the allowable shearing stress is 25 MPa. Rod AB is
nexus9112 [7]

Answer:

The solid rod BC has a diameter of 30 mm and is made of an aluminum for which the allowable shearing stress is 25 MPa. Rod AB is hollow and has an outer diameter of 25 mm; it is made of a brass for which the allowable shearing stress is 50 MPa.

4 0
2 years ago
What is the built-in pollution control system in an incinerator called
Kobotan [32]

Explanation:

hbyndbnn☝️

7 0
2 years ago
After testing a model of a fuel-efficient vehicle, scientists build a full-sized vehicle with improved fuel efficiency. Which st
Mazyrski [523]
B evaluate the solution. Sorry if I'm wrong.
4 0
3 years ago
Engineering Careers Scavenger Hunt
emmasim [6.3K]

Answer:

c

Explanation:

it's the only engineering career

6 0
3 years ago
Read 2 more answers
Sketch T-s and p-v diagrams for the Diesel cycle.
labwork [276]

Answer:

Diesel cycle:

    All diesel engine works on diesel cycle.It have four processes .These four processes are as follows

1-2.Reversible adiabatic compression

2-3.Heat addition at constant pressure

3-4.Reversible adiabatic expansion

4-1.Heat addition at constant volume

When air inters in the piston cylinder after that it compresses and gets heated due to compression after that heat addition take place at constant pressure after that power is produces when piston moves to bottom dead center.

From the diagram of P-v And T-s we can understand so easily.

3 0
3 years ago
Other questions:
  • Consider the following class definitions: class smart class superSmart: public smart { { public: public: void print() const; voi
    6·1 answer
  • Evan notices a small fire in his workplace. Since the fire is small and the atmosphere is not smoky he decides to fight the fire
    10·1 answer
  • Select the statement that is false.
    12·1 answer
  • The heat flux through a 1-mm thick layer of skin is 1.05 x 104 W/m2. The temperature at the inside surface is 37°C and the tempe
    8·1 answer
  • Which of the following units of measurement is denoted by a single apostrophe mark (')?
    6·1 answer
  • <img src="https://tex.z-dn.net/?f=%5Cint%5Climits%5Ea_b%20%7B7x%7D%20%5C%2C%20dx" id="TexFormula1" title="\int\limits^a_b {7x} \
    8·1 answer
  • We have a tube with a diameter of 5 inches that is 1 foot long. The tube then reduces the diameter to 3 inches. According to the
    8·2 answers
  • सत्य से अधिक उपयोगी एवं आज्ञापालन से श्रेष्ठ क्या है ?<br>answer fast plz​
    9·1 answer
  • A gas mixture containing 3 moles CO2, 5 moles H2 and 1 mole water is undergoing the following reactions CO2+3H2 →cH3OH + H2O Dev
    10·1 answer
  • The majority of adults now own smartphones or tablets, and most of them say they use them in part to get the news. From 2004 to
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!