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
Diana and Kinsey are put in charge of choosing a mascot for their basketball team. There are fifteen players on the team, but Di
arsen [322]

Answer:

the data are inadequate

Explanation:

<u>If there are 15 people on the team, and only five have been asked about the mascot, </u><u>this means the data collecting is wrong, and the result doesn’t include thoughts of the majority</u>. If Diana and Kinsey want to have adequate data,<u> they should ask as many as possible, if not all players in the team</u>. This would truly show what the majority wants meaning it will show what the team wants. This kind of complete data correcting is the correct one.

6 0
3 years ago
A Carnot refrigeration cycle absorbs heat at -12 °C and rejects it at 40 °C. a)-Calculate the coefficient of performance of this
tresset_1 [31]

Answer:

a)COP=5.01

b)W_{in}=2.998 KW

c)COP=6.01

d)Q_R=17.99 KW

Explanation:

Given

T_L= -12°C,T_H=40°C

For refrigeration

  We know that Carnot cycle is an ideal cycle that have all reversible process.

So COP of refrigeration is given as follows

COP=\dfrac{T_L}{T_H-T_L}  ,T in Kelvin.

COP=\dfrac{261}{313-261}

a)COP=5.01

Given that refrigeration effect= 15 KW

We know that  COP=\dfrac{RE}{W_{in}}

RE is the refrigeration effect

So

5.01=\dfrac{15}{W_{in}}

b)W_{in}=2.998 KW

For heat pump

So COP of heat pump is given as follows

COP=\dfrac{T_h}{T_H-T_L}  ,T in Kelvin.

COP=\dfrac{313}{313-261}

c)COP=6.01

In heat pump

Heat rejection at high temperature=heat absorb at  low temperature+work in put

Q_R=Q_A+W_{in}

Given that Q_A=15KW

We know that  COP=\dfrac{Q_R}{W_{in}}

COP=\dfrac{Q_R}{Q_R-Q_A}

6.01=\dfrac{Q_R}{Q_R-15}

d)Q_R=17.99 KW

5 0
4 years ago
In an aligned and continuous carbon fiber-reinforced nylon 6,6 composite, the fibers are to carry 97% of a load applied in the l
Juli2301 [7.4K]

Answer:

a) 0.26

b) 1077 MPa

Explanation:

a) The following equation can be used to determine the volume fraction:

\frac{F_f}{F_m} =\frac{E_fV_f}{E_m(1-V_f)}

\frac{0.97}{1-0.97} =\frac{260V_f}{2.8(1-V_f)}

32.3 = \frac{260V_f}{2.8-2.8V_f}

V_f = 0.26

b) Tensile strength can be found by using the following equation:

\sigma_{cl} = \sigma_m(1-V_f)+\sigma_fV_f = 50*(1-0.26)+4000*0.26 = 1077 MPa

5 0
4 years ago
Alberto's mom is taking a splinter out of his hand with a pair of tweezers. The tweezers are 3 inches long. She is applying .25
Burka [1]

Answer:

ok

Explanation:

here you go

5 0
4 years ago
Read 2 more answers
Using the data in the photo write the complex waveform expression​
UNO [17]

Answer:

1st Harmonic:

v(t) = 50\cos(2000\pi t)

3rd Harmonic:

v(t) = 9\cos(6000\pi t)

5th Harmonic:

v(t) = 6\cos(10000\pi t)

7th Harmonic:

v(t) = 2\cos(14000\pi t)

Explanation:

The general form to represent a complex sinusoidal waveform is given by

v(t) = A\cos(2\pi f t + \phi)

Where A is the amplitude in volts of the sinusoidal waveform

Where f is the frequency in cycles per second (Hz) of the sinusoidal waveform

Where \phi is the phase angle in radians of the sinusoidal waveform.

1st Harmonic:

We have A = 50, f = 1000 and φ = 0

v(t) = 50\cos(2\pi 1000 t + 0) \\\\v(t) = 50\cos(2000\pi t)

3rd Harmonic:

We have A = 9, f = 3000 and φ = 0

v(t) = 9\cos(2\pi 3000 t + 0) \\\\v(t) = 9\cos(6000\pi t)

5th Harmonic:

We have A = 6, f = 5000 and φ = 0

v(t) = 6\cos(2\pi 5000 t + 0) \\\\v(t) = 6\cos(10000\pi t)

7th Harmonic:

We have A = 2, f = 7000 and φ = 0

v(t) = 2\cos(2\pi 7000 t + 0) \\\\v(t) = 2\cos(14000\pi t)

Note: The even-numbered harmonics have 0 amplitude that is why they are not shown here.

8 0
4 years ago
Read 2 more answers
Other questions:
  • Ayden read 84 pages in 2 hours. At that rate, how many pages can he read in 5 hours
    11·2 answers
  • Are there specific building code requirements for the installation of specialized water-based fire protection systems?
    10·1 answer
  • Consider a new peer Alice that joins BitTorrent without possessing any chunks. Without any chunks,she cannot become a top-four u
    9·1 answer
  • For a statically indeterminate axially loaded member, Group of answer choices The total deflection between end A and end B of an
    6·1 answer
  • A 31.5 % hydrochloric acid solution is pumped from one storage tank to another. The power input to the pump is 2 kW and is 50% e
    12·2 answers
  • Show that -40 F is approximately equal to -40 C.
    12·1 answer
  • A 200-L tank (see Fig. P4.107) initially contains water at 100 kPa and a quality of 1%. Heat is transferred to the water, thereb
    6·1 answer
  • Three possible career opportunities in embedded systems engineering
    11·1 answer
  • In the circuit given below, R1 = 17 kΩ, R2 = 74 kΩ, and R3 = 5 MΩ. Calculate the gain 1formula58.mml when the switch is in posit
    7·1 answer
  • During the process of building titanic did titanic catch on fire and weaken the hall befor it hit the iceburg
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!