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
Which Finance jobs can someone pursue with only a high school diploma? Check all that apply.
Zolol [24]

Answer:

Teller, Loan Officer, and Tax Preparer

Explanation:

3 0
3 years ago
Read 2 more answers
A liquid refrigerant (sg=1,2) is flowing at a weight flow rate of 20,9 N/h. Refrigerant flashes into a vapor and its specific we
Iteru [2.4K]

Answer:

Explanation:

volume of 20.9 N

= 20.9 / 11.5 m³

= 1.8174 m³

In one hour 1.8174 m³ flows

in one second volume flowing = 1.8174 / 60 x 60

= 5 x 10⁻⁴ m³

Rate of volume flow = 5 x 10⁻⁴ m³ / s .

5 0
3 years ago
The inner surface of a hollow cylinder is subjected to tangential and axial stresses of 40,000 and 24,000 psi, respectively. Det
Furkat [3]

Answer:

15,000 psi

Explanation:

The solution / solving is attach below.

5 0
3 years ago
A lake has a carrying capacity of 10,000 fish. At the current level of fishing, 2,000 fish per year are taken with the catch uni
arlik [135]

Answer:

The population size would be p' = 5000

The yield would be    MaxYield = 2082 \ fishes \ per \ year

Explanation:

So in this problem we are going to be examining the application of a  population dynamics a fishing pond and stock fishing and objective would be to obtain the maximum sustainable yield and and the population of the fish at the obtained maximum sustainable yield,  so basically we would be applying an engineering solution to fishing

 

    So the current  yield which is mathematically represented as

                               \frac{dN}{dt} =   \frac{2000}{1 \ year }

 Where dN is the change in the number of fish

            and dt is the change in time

So in order to obtain the solution we need to obtain the  rate of growth

    For this we would be making use of the growth rate equation which is

                                      r = \frac{[\frac{dN}{dt}] }{N[1-\frac{N}{K} ]}

  Where N is the population of the fish which is given as 4,000 fishes

          and  K is the carrying capacity which is given as 10,000 fishes

             r is the growth rate

        Substituting these values into the equation

                              r = \frac{[\frac{2000}{year}] }{4000[1-\frac{4000}{10,000} ]}  =0.833

The maximum sustainable yield would be dependent on the growth rate an the carrying capacity and this mathematically represented as

                      Max Yield  = \frac{rK}{4} = \frac{(10,000)(0.833)}{4} = 2082 \ fishes \ per \ year

So since the maximum sustainable yield is 2082 then the the population need to be higher than 4,000 so in order to ensure a that this maximum yield the population size denoted by p' would be

                          p' = \frac{K}{2}  = \frac{10,000}{2}  = 5000\ fishes          

7 0
3 years ago
Read 2 more answers
Electric current originates from which part of an atom? *
yanalaym [24]

Answer: Electric current originates from positively charged protons negatively charged electrons of an atom.

Explanation:

The movement of ions (positive or negative) from one point to another is called electric current.

An atom has three sub-atomic particles. These are protons, neutrons and electrons.

Protons are positively charged, neutrons have no charge and electrons are negatively charged. Protons and neutrons reside inside the nucleus of an atom whereas electrons revolve around the nucleus.

So, protons and electrons are responsible for originating electric current form an atom as these are the charged particles.

Thus, we can conclude that electric current originates from positively charged protons negatively charged electrons of an atom.

3 0
2 years ago
Other questions:
  • Define a public static method named s2f that takes two String arguments, the name of a file and some text. The method creates th
    5·1 answer
  • 9) A construction company employs 2 sales engineers. Engineer 1 does the work in estimating cost for 70% of jobs bid by the comp
    11·1 answer
  • Write a function digits() that accepts a non-negative integer argument n and returns the number of digits in it’s decimal repres
    13·1 answer
  • Psychologist who uses behavioral approach to therapy would probably try which of the following
    13·2 answers
  • white Sands national monument is a large area made of white sand dunes. what is likely to be true of this area?​
    8·1 answer
  • 2.4 kg of nitrogen at an initial state of 285K and 150 kPa is compressed slowly in an isothermal process to a final pressure of
    8·1 answer
  • The value of the critical Reynolds number for the flow over a flat plate is 5 x10^5. The significance of the value is:
    13·1 answer
  • You are considering purchasing a compact washing machine, and you have the following information: The Energy Guide claims an est
    8·1 answer
  • In casting experiments performed using a certain alloy and type of sand mold, it took 155 sec for a cube-shaped casting to solid
    9·1 answer
  • What are flip flops and what do they look like
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!