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
What is the measurement of this dial caliper? <br> A. 5.491<br> B. 4.044<br> C. 5.691
just olya [345]
Its C .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... ..........
4 0
4 years ago
An aluminum alloy tube with an outside diameter of 3.50 in. and a wall thickness of 0.30 in. is used as a 14 ft long column. Ass
slega [8]

Answer:

slenderness ratio = 147.8

buckling load = 13.62 kips

Explanation:

Given data:

outside diameter is 3.50 inc

wall thickness 0.30 inc

length of column is 14 ft

E = 10,000 ksi

moment of inertia = \frac{\pi}{64 (D_O^2 -D_i^2)}

I = \frac{\pi}{64}(3.5^2 -2.9^2) = 3.894 in^4

Area = \frac{\pi}{4} (3.5^2 -2.9^2) = 3.015 in^2

radius = \sqrt{\frac{I}{A}}

r = \sqrt{\frac{3.894}{3.015}

r = 1.136 in

slenderness ratio = \frac{L}{r}

                              = \frac{14 *12}{1.136} = 147.8

buckling load = P_cr = \frac{\pi^2 EI}}{l^2}

P_{cr} = \frac{\pi^2 *10,000*3.844}{( 14\times 12)^2}

P_{cr} = 13.62 kips

3 0
3 years ago
Two steel plates are to be held together by means of 16-mm-diameter high-strength steel bolts fitting snugly inside cylindrical
dusya [7]

Answer:

The outer diameter of the spacers that yields the most economical and safe design is 25.03 mm

Explanation:

For steel bolt

Stress = 210 MPa or 210 N/mm2

Pressure = Stress* Area

Pbolt = 210 N/mm2 * 16^2 *(pi)/4

Pbolt = 210 N/mm2 * 200.96 mm^2 = 42201.6  N

For Brass spacer

Pressure = 42201.6  N

Area of Brass spacer = Pressure/Stress

Area of Brass spacer = 42201.6  N/145 N/mm^2 = 291.044 mm^2

Area of Brass spacer = (pi) (d^2 - 16^2)/4 =  291.044 mm^2

d^2 - 16^2 = 291.044 mm^2* 4/(pi) = 370.758

d^2 =  370.758 + 16^2

d^2 =   626.758

d = 25.03 mm

The outer diameter of the spacers that yields the most economical and safe design is 25.03 mm

5 0
3 years ago
If you are unsure about holding a piece of wood to be drilled, then you should always use a
alisha [4.7K]
C I took construction class
4 0
3 years ago
Which system of linear inequalities is represented by the graph? y &gt; x – 2 and y x + 1 y x + 1 y &gt; x – 2 and y &lt; x + 1
KatRina [158]

Answer:

The graph representing the linear inequalities is attached below.

Explanation:

The inequalities given are :

y>x-2   and y<x+1

For tables for values of x and y and get coordinates to plot for both equation.

In the first equation;

y>x-2

y=x-2

y-x = -2

The table will be :

x    y

-2  -4

-1    -3

0     -2

1      -1

2      0

The coordinates to plot are : (-2,-4) , (-1,-3), (0,-2), (1,-1) ,(2,0)

Use a dotted line and shade the part right hand side of the line.

Do the same for the second inequality equation and plot then shade the part satisfying the inequality.

The graph attached shows results.

5 0
4 years ago
Read 2 more answers
Other questions:
  • ¿Por qué creen que la Ingeniería Metalúrgica es una carrera estratégica para el desarrollo de nuestro país?
    9·1 answer
  • Eutectic product in Fe-C system is called A. Pearlite B. Bainite C. Ledeburite D. Spheroidite
    13·1 answer
  • A wooden cylinder (0 02 x 0 02 x 0 1m) floats vertically in water with one-third of ts length immersed. a)-Determine the density
    7·1 answer
  • In an experiment, the local heat transfer over a flat plate were correlated in the form of local Nusselt number as expressed by
    5·1 answer
  • Where can you find free air pods that look real
    8·1 answer
  • Why won't Brainly let me make a account or log in? It's always telling me that it can't take my registration at this time or it
    9·1 answer
  • Help me is it a b c or d?
    14·1 answer
  • A demand factor of _____ percent applies to a multifamily dwelling with ten units if the optional calculation method is used.
    14·1 answer
  • 8. Find the volume of the figure shown below: * V=L x W x H 7 cm 2 cm 2 cm​
    9·1 answer
  • Tech A says that speed density systems use vehicle speed and fuel density to determine injector pulse width. Tech B says that ma
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!