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
1. advantages of 2 pulley system
n200080 [17]

Answer:

Advantages

The main advantage in the use of pulleys is that the effort becomes less as compared to the normal lifting of the weights. In other words, it reduces the amount of actual force required to lift heavy objects. It also changes the direction of the force applied. These two advantages in the use pulleys make them an important tool for heavy lifting. It also provides a mechanical advantage.

The other advantage in the use of pulleys is that the distance between the operator and weight. There is a safe distance between them which avoids any disaster. Pulleys are easy to assemble and cost-effective. The combination of different directional pulleys can change the position of the load with little effort. Though there are moving parts in the pulley system they require less or no lubrication after installation.

Disadvantages

Apart from the above-said advantages while using pulley systems, there are several disadvantages in their use. The main disadvantage in the use of the pulley system is that it requires large space to install and operate. The mechanical advantage of pulleys can go to higher values but need more space to install them.

In some cases, the ropes/belts move over the wheel with no grooves, the chances of the slip of ropes/belts from the wheel are inevitable. If the system is installed to use for a long time, they require maintenance and regular check-up of ropes/cables as the friction between the wheels and cables/ropes occur causing wear and tear to them. Continuous use of the system makes the ropes weak. The rope may break while using the system causing damages to the operator, surrounding place and the load which is being lifted.

5 0
3 years ago
The development of various technologies led to many historic events. Use information from the Internet to describe one major his
11111nata11111 [884]

Answer:

1. Industrial revolution was initiated or borne through the production of Steel

2. World War 1 led to the development of Tanks

Explanation:

The production of Steel through the Bessemer Process in the middle of the nineteenth century was a major technological development that spurred the Industrial revolution. This invention led to the widespread use of steel in the production of many things including vehicles and airplanes.

During the First World War in 1914, soldiers found the use of just their armaments in battle as not so productive. This led to the development of Tanks in 1915 that would continue moving towards the enemy even when being shot at.

4 0
3 years ago
1. You should
vladimir2022 [97]
D
Step by step explanation
3 0
2 years ago
Read 2 more answers
Consider a CMOS inverter which has ideal transistors with the following characteristics: PMOS transistor: W/L = 2; Mobility (up)
yaroslaw [1]

Answer:

The transistor will be in amplifier mode and we will expect current will be higher than expected.

6 0
3 years ago
For the following gear train, if the blue gear is moving at 50 rpm, what are the speeds of the other gears?
Flauer [41]

Answer:

6

Explanation:

6 teddy bears

5 0
3 years ago
Other questions:
  • Consider a unidirectional continuous fiber-reinforced composite with epoxy as the matrix with 55% by volume fiber.i. Calculate t
    10·1 answer
  • A 356 cast aluminum test bar is tested in tension. The initial gage length as marked on the sample is 50mm and the initial diame
    9·1 answer
  • Define the difference between elastic and plastic deformation in terms of the effect on the crystal lattice structure.
    5·1 answer
  • Atmospheric pressure is measured to be 14.769 psia. a. What would be the equivalent reading of a water barometer (inches of H20)
    11·1 answer
  • 3. Write down the total thermal resistance for a double-pipe heat exchanger. Show how to convert from total resistance to an ove
    12·2 answers
  • Consider a rectangular fin that is used to cool a motorcycle engine. The fin is 0.15m long and at a temperature of 250C, while t
    5·1 answer
  • A flashed steam geothermal power plant is located where underground hot water is available as saturated liquid at 700 kPa. The w
    14·1 answer
  • Which of the following is an essential component of reinforced concrete?
    9·1 answer
  • Which Two moon phases are directly opposite each other?
    9·2 answers
  • GOOD AFTERNOON GUYSS!! ​
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!