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
Writing Prompt 2: Split Decisions PLEASE HELP
notsponge [240]

Answer:

Explanation:

is there a book or smg

4 0
3 years ago
The lid on a pressure vessel is held down with 10 bolts that pass through the lid and a flange on the pressure vessel (similar t
Volgvan

Answer:

25120 N

Explanation:

The external load acting on each bolted joint

= P x A / N

= (8 x 10⁶) (31.4 x 10³ x 10⁻⁶) / 10

= 25120 N

4 0
4 years ago
Five bolts are used in the connection between the axial member and the support. The ultimate shear strength of the bolts is 280
stiks02 [169]

Answer:

Minimum Allowable diameter for each of the 5 bolts = 0.0394 m = 3.94 cm

Explanation:

Maximum Working Stress = Ultimate Shear Stress/factor of safety

Maximum Working Stress = 280 MPa/3.8 = 73.68 MPa

Working Stress = Applied load/Minimum allowable Area = L/A

Minimum Allowable Area = Applied Load/Maximum Working Stress

A = 450000/73680000 = 0.00611 m²

This area is supplied by 5 bolts, so each bolt supplies A/5 = 0.0061/5 = 0.00122 m²

Cross sectional Area of bolts = πD²/4

0.00122 = πD²/4

D² = 4 × 0.00122/π = 0.00155

D = √0.00155 = 0.0394 m = 3.94 cm

Each of the five bolt can have a minimum diameter of 3.94 cm

Hope this Helps!!!

8 0
3 years ago
As a general rule of thumb, the ratio of the rate of etch-product formation to the flow rate of etch gas should be greater than
g100num [7]

Answer:

The solution and complete explanation for the above question and mentioned conditions is given below in the attached document.i hope my explanation will help you in understanding this particular question.

Explanation:

8 0
3 years ago
the hoop is cast on the rough surface such that it has an angular velocity w=4rad/s and an angular acceleration a=5rad/s^2. also
IgorLugansk [536]

Given Information:

Angular velocity = ω = 4 rad/s

Angular acceleration = α = 5 rad/s²

Center deceleration = a₀ = 2 m/s

Required Information:

Acceleration of point A at this instant = ?

Answer:

Acceleration of point A at this instant = 5.94 m/s²

Explanation:

Refer to the attached diagram of the question,

The acceleration of point A is given by

a = a₀ + rα - rω²

Where r is the radial distance between the center and point A, a₀ is the deceleration of center, α is the angular acceleration and ω is the angular velocity.

a = -2i + 0.3j*5k - 0.3j*4²

a = -2i + 1.5(j*k) - 0.3j*16

a = -2i + 1.5(-i) - 4.8j

a = -2i - 1.5i - 4.8j

a = -3.5i - 4.8j

The magnitude of acceleration vector is

a = √(-3.5)² + (-4.8)²

a = √35.29

a = 5.94 m/s²

Therefore, the acceleration of point A is 5.94 m/s²

The angle is given by

θ = tan⁻¹(y/x)

θ = tan⁻¹(-4.8/-3.5)

θ = 53.9°

7 0
3 years ago
Other questions:
  • A power hacksaw used to cut metal, Link 5 pivots at O5 and its weight forces the saw-blade against the workpiece while the linka
    11·1 answer
  • A vehicle has a crimped fuel return line. What effect will this have on 5-Gas analyzer gas readings?
    5·2 answers
  • What is the movement of the piston from top dead center (TDC) to bottom dead center (BDC) called?
    9·1 answer
  • a triangle is defined by the three vertices. write the following functions of the triangle class assume that the point class has
    7·1 answer
  • Vernier calipers are capable of taking readings to the nearest 0.001 in. a)- True b)- false
    5·1 answer
  • Both a gage and a manometer are attached to a gas tank to measure its pressure. If the reading on the pressure gage is 65 kPa, d
    9·1 answer
  • When you an angle you divide it into two equal part s
    13·2 answers
  • Nơi nào có điện tích thì xung quanh điện tích đó có :
    9·1 answer
  • You are flying a Cessna 182 Skylane at a cruise speed of 140 KTS. You are flying on a trip that will take you 520 NM. How long w
    9·1 answer
  • What is not a factor of 25​
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!