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
Two routes connect an origin and a destination. Routes 1 and 2 have performance functions t1 = 2 + X1 and t2 = 1 + X2, where the
Musya8 [376]

Solution :

Given

$t_1=2+x_1$

$t_2=1+x_2$

Now,

$P(h

$0.4=1-P(h \geq5)$

$0.6=P(h \geq5)$

$0.6= e^{\frac{-x_1 5}{3600}}$

Therefore,   $x_1=368 \ veh/h$

                        $=\frac{368}{1000} = 0.368$

Given,   $t_1=2+x_1$

                 = 2 + 0.368

                 = 2.368 min

At user equilibrium, $t_2=t_1$

∴  $t_2$ = 2.368 min

$t_2=1+x_2$

$2.368=1+x_2$

$x_2 = 1.368$

$x_2 = 1.368 \times 1000$

    = 1368 veh/h

7 0
3 years ago
In a surface grinding operation, the wheel diameter = 8.0 in, wheel width = 1.0 in, wheel speed = 6000 ft/min, work speed = 40 f
Katen [24]

Answer: the answer will be d because it is the right one to be

Explanation:

7 0
3 years ago
Create a separate function file fieldtovar.m that receives a single structure as an input and assigns each of the field values t
Soloha48 [4]

Answer:

Explanation gives the answer

Explanation:

% Using MATLAB,

% Matlab file : fieldtovar.m

function varargout = fieldtovar(S)

% function that accepts single structure as input, assigning each

% of the field values to user-defined variables

fields = fieldnames(S); % get the field names of the input structure

% check if number of user-defined variables and number of fields in

% structure are equal

if nargout == length(fields)

% if equal assign each value of structure to user-defined varable

for i=1:nargout

varargout{i} = getfield(S,fields{i});

end

else

% if not equal display an error message

error('The number of output variables does not equal the number of fields');

end

end

%This brings an end to the program

4 0
3 years ago
Technician A ay that acid rain doe the mot harm when it firt fall on a finih. Technician B ay that hard water potting i uually j
Tamiku [17]

Technician B is right say that hard water potting i usually jut a Surface problem that can be wahed off.

What do you mean by Hard water?

The amount of dissolved calcium and magnesium in the water determines its hardness. Calcium and magnesium are the main dissolved minerals in hard water. The last time you washed your hands, you might have actually felt the effects of hard water.

What do you mean by acid rain?

Any type of precipitation that contains acidic elements, such as sulfuric or nitric acid, that falls to the ground from the atmosphere in wet or dry forms is referred to as acid rain, also known as acid deposition. Rain, snow, fog, hail, and even acidic dust can fall under this category.

Some plants are sensitive to excessive moisture around their root zone, so it may be necessary to increase drainage when growing plants in pots. Additionally, standing water at the bottom of the pot can cause root rot.

Many university agriculture extension agencies have thoroughly debunked the old garden myth that adding rocks to the bottom of a pot will increase drainage.

Learn more about hard water click here:

brainly.com/question/28178305

#SPJ4

6 0
1 year ago
A structural component in the form of a wide plate is to be fabricated from a steel alloy that has a plane-strain fracture tough
jeyben [28]

Complete question:

A structural component in the form of a wide plate is to be fabricated from a steel alloy that has a plane strain fracture toughness of 98.9 MPa root m (90 ksi root in.) and a yield strength of 860 MPa (125,000 psi). The flaw size resolution limit of the flaw detection apparatus is 3.0 mm (0.12 in.). If the design stress is one-half of the yield strength and the value of Y is 1.0, determine whether or not a critical flaw for this plate is subject to detection.

Answer:

Since the flaw 17mm is greater than 3 mm the critical flaw for this plate is subject to detection

so that critical flow is subject to detection  

Explanation:

We are given:

Plane strain fracture toughness K = 98.9 MPa \sqrt{m}

Yield strength Y = 860 MPa

Flaw detection apparatus = 3.0mm (12in)

y = 1.0

Let's use the expression:

oc = \frac{K}{Y \sqrt{pi * a}}

We already know

K= design

a = length of surface creak

Since we are to find the length of surface creak, we will make "a" subject of the formula in the expression above.

Therefore

a= \frac{1}{pi} * [\frac{k}{y*a}]^2

Substituting figures in the expression above, we have:

= \frac{1}{pi} * [\frac{98.9 MPa \sqrt{m}} {10 * \frac{860MPa}{2}}]^2

= 0.0168 m

= 17mm

Therefore, since the flaw 17mm > 3 mm the critical flow is subject to detection  

3 0
3 years ago
Read 2 more answers
Other questions:
  • A six-lane freeway (three lanes in each direction) currently operates at maximum LOS C conditions. The lanes are 11 ft wide, the
    5·1 answer
  • Which of the following is not an example of heat generation? a)- Exothermic chemical reaction in a solid b)- Endothermic Chemica
    15·1 answer
  • Write a program that uses while loops to perform the following steps: Step a: Prompt the user to input two integers: firstNum an
    12·1 answer
  • How does refrigeration preserve food and dead bodies​
    12·2 answers
  • A wine aerator is a small, in-bottle, hand-held pour-through or decantor top device using the venturi effect for aerating the wi
    9·1 answer
  • the voltage across a 5mH inductor is 5[1-exp(-0.5t)]V. Calculate the current through the inductor and the energy stored in the i
    6·1 answer
  • Ask a question about your assignment
    8·2 answers
  • Give five examples of
    14·1 answer
  • A continuous function y = ƒ(x) is known to be negative at x = 0 and positive at x = 1. Why does the equation ƒ(x) = 0 have at le
    14·1 answer
  • A 9 -slug mass hangs by a rope from the ceiling. Using the standard value of gravitational acceleration g = 32.2 fts 2, what is
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!