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
A 20.0 µF capacitor is charged to a potential difference of 800 V. The terminals of the charged capacitor are then connected to
Sergeu [11.5K]

Answer:

a) Q_initial = 16 * 10^-3 C

b) V_1 = V_2 =  (16/3) * 10^2 V

c)  E = 64/15 J

d)  dE = 32/15 J of decrease

Explanation:

Given:

- Capacitor 1, C_1 = 20.0 uF

- Capacitor 2, C_2 = 10.0 uF

- Charged with P.d V = 800 V

Find:

a) the original charge of the system,

(b) the final potential difference across each capacitor

(c) the final energy of the system

(d) the decrease in energy when the capacitors are connected.

Solution:

a)

- The initial charge in the circuit is the one carried by the first charged capacitor.

                           Q_initial = C_1*V

                           Q_initial = 20*10^-6 * 800

                           Q_initial = 16 * 10^-3 C

b)

- After charging the other capacitor, we know that the total charge is conserved among two capacitor:

                          Q_initial = Q_1 + Q_2

- We also know that potential difference across two capacitor is also same.

                          V_1 = V_2 = Q_1 / C_1 = Q_2 / C_2

- Using the two equations and solve for charge Q_2:

                          Q_2 = Q_1*C_2/C_1

                          Q_2 = Q_1*10/20 = 0.5*Q_1

- using conservation of charge:

                          Q_initial = 1.5*Q_1

                          Q_1 = 16*10^-3 / 1.5 = 10.67*10^-3 C

- Hence the Voltage across each capacitor is:

                          V_2 = V_1 = Q_1 / C_1  

                                            = 10.67*10^-3 / 20*10^-6

                                            = (16/3) * 10^2 V

c)

- The energy in the system is:

                          E = 0.5*C_eq*V^2

Where, C_eq is the equivalent capacitance of paralle circuit.

                           E = 0.5*(20+10)*10^-6 *((16/3) * 10^2)^2

                          E = 64/15 J

d)

- The decrease in energy of the capacitors is:

                           dE = E_initial - E_final

Where, E_initial is due to charging of the C_1 only:

                          dE = 0.5*10^-6*20*800^2 - (64/15)

                          dE = 32/5 - 64/15 = 32/15 J

5 0
3 years ago
You are designing the access control policies for a Web-based retail store. Customers access the store via the Web, browse produ
PolarNik [594]

Answer:

Object-Oriented Software Engineering Using UML, Patterns, and Java, 3e, shows readers how to use both the principles of software engineering and the practices of various object-oriented tools, processes, and products.

3 0
2 years ago
Help now please evaluate using the commutative property: 40 (32) (10) (25)
Ilia_Sergeevich [38]

Answer:

#See solution for details.

Explanation:

#The commutative property of multiplication tells us that it doesn't matter in what order you multiply numbers. The formula for this property is a * b = b * a:

40\times32\times10\times 25=320,000\\\\25\times10\times32\times 40=320,000\\\\10\times32\times25\times 40=320,000\\\\32\times25\times40\times 10=320,000

Hence, the product of the four numbers remains 320,000 irrespective of their order.

4 0
3 years ago
The capacity of a battery is 1800 mAh and its OCV is 3.9 V. a) Two batteries are placed in series. What is the combined battery
Lynna [10]

Answer:

capacity  = 0.555 mAh

capacity  = 3600 mAh

Explanation:

given data

battery = 1800 mAh

OCV = 3.9 V

solution

we get here capacity when it is in series

so here Q = 2C  

capacity  = 2 × ampere × second   ...............1

put here value and we get

and 1 Ah = 3600 C

capacity  = \frac{2}{3600}

capacity  = 0.555 mAh

and

when it is in parallel than capacity will be

capacity = Q1 +Q2   ...............2

capacity  = 1800 + 1800

capacity  = 3600 mAh

3 0
3 years ago
Sketch T-s and p-v diagrams for the Diesel cycle.
labwork [276]

Answer:

Diesel cycle:

    All diesel engine works on diesel cycle.It have four processes .These four processes are as follows

1-2.Reversible adiabatic compression

2-3.Heat addition at constant pressure

3-4.Reversible adiabatic expansion

4-1.Heat addition at constant volume

When air inters in the piston cylinder after that it compresses and gets heated due to compression after that heat addition take place at constant pressure after that power is produces when piston moves to bottom dead center.

From the diagram of P-v And T-s we can understand so easily.

3 0
3 years ago
Other questions:
  • If a torque of M = 300 N⋅m is applied to the flywheel, determine the force that must be developed in the hydraulic cylinder CD t
    13·1 answer
  • You want to determine whether the race of the defendant has an impact on jury verdicts. You assign participants to watch a trial
    9·1 answer
  • What material resources and intellectual resources were used in self driving cars?
    15·1 answer
  • One gram of Strontium-90 has an activity of 5.3 terabecquerels (TBq), what will be the activity of 1 microgram?
    8·1 answer
  • How do I cancel my subscription
    12·2 answers
  • Two technicians are discussing a vehicle that will not start. Tech A states that a problem with the immobilizer system may be th
    9·1 answer
  • The mechanical advantage of a screw is always ____________________ than/to 1. Question 5 options: less, greater, equal, none of
    7·1 answer
  • The pressure intensity at a point in a fluid is equal in all directions a.true b.false​
    12·1 answer
  • URGENT PLEASE HELP!!!
    11·1 answer
  • Tech A says that coolant circulates through some intake manifolds to help warm them up. Tech B says that some intake manifolds u
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!