Guest User

Untitled

a guest
Oct 4th, 2023
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.13 KB | None | 0 0
  1. //normal
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import java.util.Scanner;
  6. import java.util.concurrent.ThreadLocalRandom;
  7.  
  8. public class BirdFeedingSimulator {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         System.out.print("Enter the number of grains: ");
  13.         int grainsOfWheat = scanner.nextInt();
  14.  
  15.         System.out.print("Enter the number of small stones: ");
  16.         int smallStones = scanner.nextInt();
  17.  
  18.         System.out.print("Enter the number of birds: ");
  19.         int totalBirds = scanner.nextInt();
  20.  
  21.         int maxBirdsAtPlate = 6;
  22.  
  23.         // Create a queue to represent birds waiting to eat
  24.         Queue<Integer> birdsQueue = new LinkedList<>();
  25.         for (int i = 1; i <= totalBirds; i++) {
  26.             birdsQueue.offer(i);
  27.         }
  28.  
  29.         int[] birdResults = new int[totalBirds]; // 0 for not full, 1 for half full, 2 for full
  30.  
  31.         while (!birdsQueue.isEmpty()) {
  32.             int birdsAtPlate = Math.min(maxBirdsAtPlate, birdsQueue.size());
  33.  
  34.             for (int i = 0; i < birdsAtPlate; i++) {
  35.                 int birdId = birdsQueue.poll();
  36.                 int attempts = 2;
  37.                 int grainsEaten = 0;
  38.                 int stonesEaten = 0;
  39.  
  40.                 while (attempts > 0) {
  41.                     int randomChoice = ThreadLocalRandom.current().nextInt(2);
  42.                     if (randomChoice == 0 && grainsOfWheat > 0) {
  43.                         grainsEaten++;
  44.                         grainsOfWheat--;
  45.                     } else if (randomChoice == 1 && smallStones > 0) {
  46.                         stonesEaten++;
  47.                         smallStones--;
  48.                     }
  49.                     attempts--;
  50.                 }
  51.  
  52.                 if (grainsEaten == 2) {
  53.                     birdResults[birdId - 1] = 2; // Full
  54.                 } else if (grainsEaten == 1 && stonesEaten == 1) {
  55.                     birdResults[birdId - 1] = 1; // Half Full
  56.                 } else if (stonesEaten == 2) {
  57.                     birdResults[birdId - 1] = 0; // Not Full
  58.                 }
  59.             }
  60.         }
  61.  
  62.         int fullCount = 0;
  63.         int halfFullCount = 0;
  64.         int notFullCount = 0;
  65.  
  66.         for (int result : birdResults) {
  67.             if (result == 2) {
  68.                 fullCount++;
  69.             } else if (result == 1) {
  70.                 halfFullCount++;
  71.             } else {
  72.                 notFullCount++;
  73.             }
  74.         }
  75.  
  76.         System.out.println("Full Birds: " + fullCount);
  77.         System.out.println("Half Full Birds: " + halfFullCount);
  78.         System.out.println("Not Full Birds: " + notFullCount);
  79.  
  80.         scanner.close(); // Close the scanner
  81.     }
  82. }
  83.  
  84.  
  85.  
  86. //web server
  87.  
  88. import com.sun.net.httpserver.HttpServer;
  89. import com.sun.net.httpserver.HttpHandler;
  90. import com.sun.net.httpserver.HttpExchange;
  91. import java.io.IOException;
  92. import java.io.OutputStream;
  93.  
  94. public class BirdFeedingWebService {
  95.     public static void main(String[] args) throws IOException {
  96.         HttpServer server = HttpServer.create();
  97.         server.bind(new InetSocketAddress(8080), 0);
  98.         server.createContext("/simulate", new SimulationHandler());
  99.         server.start();
  100.         System.out.println("Server started on port 8080...");
  101.     }
  102.  
  103.     static class SimulationHandler implements HttpHandler {
  104.         @Override
  105.         public void handle(HttpExchange exchange) throws IOException {
  106.             // Parse query parameters from the request
  107.             String query = exchange.getRequestURI().getQuery();
  108.             String[] params = query.split("&");
  109.             int grainsOfWheat = Integer.parseInt(params[0].split("=")[1]);
  110.             int smallStones = Integer.parseInt(params[1].split("=")[1]);
  111.             int totalBirds = Integer.parseInt(params[2].split("=")[1]);
  112.  
  113.             // Simulate bird feeding (you can use the previous code here)
  114.  
  115.             // Prepare the response
  116.             String response = "Full Birds: " + fullCount + "\n" +
  117.                               "Half Full Birds: " + halfFullCount + "\n" +
  118.                               "Not Full Birds: " + notFullCount;
  119.  
  120.             exchange.sendResponseHeaders(200, response.getBytes().length);
  121.             OutputStream os = exchange.getResponseBody();
  122.             os.write(response.getBytes());
  123.             os.close();
  124.         }
  125.     }
  126. }
  127.  
  128.  
  129. //  web client
  130.  
  131. import java.io.BufferedReader;
  132. import java.io.IOException;
  133. import java.io.InputStreamReader;
  134. import java.net.HttpURLConnection;
  135. import java.net.URL;
  136.  
  137. public class BirdFeedingWebServiceClient {
  138.     public static void main(String[] args) throws IOException {
  139.         int grainsOfWheat = 50;
  140.         int smallStones = 50;
  141.         int totalBirds = 50;
  142.  
  143.         String urlString = "http://localhost:8080/simulate?grains=" + grainsOfWheat +
  144.                            "&stones=" + smallStones + "&birds=" + totalBirds;
  145.  
  146.         URL url = new URL(urlString);
  147.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  148.         connection.setRequestMethod("GET");
  149.  
  150.         int responseCode = connection.getResponseCode();
  151.         if (responseCode == 200) {
  152.             BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  153.             String inputLine;
  154.             StringBuilder response = new StringBuilder();
  155.  
  156.             while ((inputLine = in.readLine()) != null) {
  157.                 response.append(inputLine);
  158.                 response.append("\n");
  159.             }
  160.  
  161.             in.close();
  162.             System.out.println(response.toString());
  163.         } else {
  164.             System.out.println("HTTP GET request failed with response code " + responseCode);
  165.         }
  166.     }
  167. }
  168.  
  169.  
  170. //webservice logic
  171.  
  172. import javax.jws.WebMethod;
  173. import javax.jws.WebParam;
  174. import javax.jws.WebService;
  175.  
  176. @WebService
  177. public class BirdFeedingService {
  178.     @WebMethod
  179.     public FeedingResponse simulateFeeding(
  180.         @WebParam(name = "grains") int grains,
  181.         @WebParam(name = "stones") int stones,
  182.         @WebParam(name = "birds") int totalBirds
  183.     ) {
  184.         int maxBirdsAtPlate = 6;
  185.         Queue<Integer> birdsQueue = new LinkedList<>();
  186.         for (int i = 1; i <= totalBirds; i++) {
  187.             birdsQueue.offer(i);
  188.         }
  189.  
  190.         int[] birdResults = new int[totalBirds]; // 0 for not full, 1 for half full, 2 for full
  191.  
  192.         while (!birdsQueue.isEmpty()) {
  193.             int birdsAtPlate = Math.min(maxBirdsAtPlate, birdsQueue.size());
  194.  
  195.             for (int i = 0; i < birdsAtPlate; i++) {
  196.                 int birdId = birdsQueue.poll();
  197.                 int attempts = 2;
  198.                 int grainsEaten = 0;
  199.                 int stonesEaten = 0;
  200.  
  201.                 while (attempts > 0) {
  202.                     int randomChoice = ThreadLocalRandom.current().nextInt(2);
  203.                     if (randomChoice == 0 && grains > 0) {
  204.                         grainsEaten++;
  205.                         grains--;
  206.                     } else if (randomChoice == 1 && stones > 0) {
  207.                         stonesEaten++;
  208.                         stones--;
  209.                     }
  210.                     attempts--;
  211.                 }
  212.  
  213.                 if (grainsEaten == 2) {
  214.                     birdResults[birdId - 1] = 2; // Full
  215.                 } else if (grainsEaten == 1 && stonesEaten == 1) {
  216.                     birdResults[birdId - 1] = 1; // Half Full
  217.                 } else if (stonesEaten == 2) {
  218.                     birdResults[birdId - 1] = 0; // Not Full
  219.                 }
  220.             }
  221.         }
  222.  
  223.         int fullCount = 0;
  224.         int halfFullCount = 0;
  225.         int notFullCount = 0;
  226.  
  227.         for (int result : birdResults) {
  228.             if (result == 2) {
  229.                 fullCount++;
  230.             } else if (result == 1) {
  231.                 halfFullCount++;
  232.             } else {
  233.                 notFullCount++;
  234.             }
  235.         }
  236.  
  237.         FeedingResponse response = new FeedingResponse();
  238.         response.setFullBirds(fullCount);
  239.         response.setHalfFullBirds(halfFullCount);
  240.         response.setNotFullBirds(notFullCount);
  241.  
  242.         return response;
  243.     }
  244. }
  245.  
  246.  
  247.  
Advertisement
Add Comment
Please, Sign In to add comment