-2

I am facing trouble to submit a code in a coding platform.

Problem Statement

A local musician is putting on a concert to raise money for charity. The concert will be held in the town hall, a spacious venue perfectly suited for such an event.

  • There are r rows of seats, each containing exactly s seats.
  • At most one person can sit on a single seat (that is, two people cannot share a seat).

There is a problem - the concert may have been overbooked! This means that if everybody who bought tickets comes to the concert, some of them might have to stand.

Now the musician has approached you, not for advice, but for the answer to the following question:

if everybody who bought tickets arrives and tries to find a seat, how many people will end up sitting, and how many people will be standing?

This is my program:

  package Prerequisite;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.PrintStream;
  import java.util.Scanner;

  public class Solution {
  static private  final String INPUT ="sitin.txt";   
  static private  final String OUTPUT ="sitout.txt";   
  public static void main(String[] args) {
    FileInputStream instream = null;  
    PrintStream outstream = null;  
      try  {  
          instream =  new  FileInputStream (INPUT);  
          outstream =  new  PrintStream ( new  FileOutputStream (OUTPUT));  
          System.setIn (instream);  
          System.setOut (outstream);  
      }  catch  (Exception e) {  
          System.err.println ( "Error Occurred." );  
      }
     Scanner sc=new Scanner(System.in);
     int r=sc.nextInt();
    int s=sc.nextInt();
    int ticket=sc.nextInt();
    if((r*s)>=ticket)
   {
   System.out.println(ticket+" "+0);
    }
  else
  {
    System.out.println(r*s+" "+(ticket-r*s));
  }
   }
     }

Input 7 12 100

Output 84 16

  • 2
    You haven't even attempted to solve the problem nor have you provided an example of the contents from the file. – RStevoUK Feb 10 '21 at 20:31
  • Welcome to SO! I re-formatted your post (currently pending in queue). Please [edit] and post your __input example__ (text contents of `sitin.txt`). What is the __expected output__, can you give an example too (at least the format)? – hc_dev Feb 10 '21 at 20:38
  • I m sorry That i not shown my attempt as it wrong so i think not to shown – 19UEC089 Mudit Tiwari Feb 10 '21 at 21:13
  • 2
    @hc_dev, please note from the [FAQs on editing](https://meta.stackoverflow.com/questions/303219/how-do-i-make-a-good-edit) the paragraph entitled 'Don't polish turds'. With all due respect to the asker, this is just a dump of a homework question, and so should be closed, not edited. – DaveyDaveDave Feb 10 '21 at 21:14
  • Actually they not provide the content of sitin.txt i can give u the site where i found this problem https://orac.amt.edu.au/cgi-bin/train/problem.pl?problemid=342&set=simple1 – 19UEC089 Mudit Tiwari Feb 10 '21 at 21:16
  • @DaveyDaveDave I m new to programming so i did not understand how to take input from external file so thats why i want to know this. – 19UEC089 Mudit Tiwari Feb 10 '21 at 21:18
  • @19UEC089MuditTiwari, understood. Try reading [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) for some useful tips on how to ask questions that will get useful answers. – DaveyDaveDave Feb 10 '21 at 21:27
  • Ok i understand that but can u solve this for me please as i want to learn this so desperately please – 19UEC089 Mudit Tiwari Feb 10 '21 at 21:31
  • 1
    @19UEC089MuditTiwari I'm afraid not, for 2 reasons: firstly, I don't have time; secondly how does me giving you the answer help you to learn in any way? Breaking the problem down into small component problems, and then carefully and methodically understanding and solving each individual problem, until you've solved the whole thing is literally all that programming is. Just being told the answer to this question won't help you one bit. – DaveyDaveDave Feb 10 '21 at 22:51
  • @DaveyDaveDave Thanks for the link To the desperate EDU newbies: Programming means research, experimentation, decomposition and communication. Grow with the next step – hc_dev Feb 10 '21 at 23:43
  • @DaveyDaveDave I did not want the answer I just want to know how to read a input from the given file and write output to that give output file so please help on this – 19UEC089 Mudit Tiwari Feb 11 '21 at 03:44
  • @19UEC089MuditTiwari Have you read [ask]? Did you research your question? I found an [answer](https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file).You can try that and if struggling, ask again – hc_dev Feb 12 '21 at 19:01
  • @hc_dev I also doubt in 3 how to write in output file – 19UEC089 Mudit Tiwari Feb 13 '21 at 20:20

1 Answers1

0

When I was a 16 years, I started programming with pseudo-code: structograms, a kind of visualization of logical flow. This may help to solve the decomposition by divide & conquer.

But first of all start defining your given inputs & expected output - call it your User-Interface (here: file-based)

Requirements: input/output interface

Supposed given Input

You are given a file with a line containing 3 records delimited by a space-character:

7 12 100

Above 3 records represent numbers which are read into variables as follows:

  • 7 rows in the concert hall
  • 12 seats in each row of the hall
  • 100 tickets are sold in total for the concert

Expected Output

The program should write its output to a file. So that this file contains a single line with 2 records, i.e numbers delimited by space (similar to given input format). First the resulting total count of occupied seats. Second the remaining count of left over visitors that can't be seated and are standing. For example:

84 16

Decomposition into Pseudo code

I can't draw a structogram here. And for your task amd language (Java), I advise to start with a couple of comments. Just write down like a movie-plot with separate scenes (input, processing, output) what should happen. Like this:

// (1) read parameters (3 variables) from input (file):
   // open given input-file (sitin.txt)
   // read first line from file
   // close file
   String content = Files.readString(path, StandardCharsets.US_ASCII);
   // split the read line by delimiter space into 3 strings (e.g. array)
   // convert the 3 strings into number variables (e.g. rows, seats, tickets

//  (2) calculate maximum seats (capacity) and ticket-category distribution (seated VS standing):
  // do the math ..
  // return the results (e.g. an object with two fields/variables: totalSeated, totalStanding)

// (3) write results to output:
   // open given output-file (sitout.txt)
   // write 3 variables delimited by space to the file
   // close file

You now have divided the problem into 3 blocks. Each can be solved separately. Thus you can structure the program into 3 methods.

Why decomposition?

It might be your key to solve any problem. It's famously known and widely-practiced by following patterns/principles: separation of concerns, single responsibility principle or simply divide & conquer.

It has many benefits: isolated development and isolated testing. In this case: you can ask 3 separate questions on stackoverflow

For (1) I have already found an answer: File-Reading, I already inserted between comments above.

Which method is it you have tried to code? Where do you need our help: 1, 2 or 3?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • You see, I have made errors above. That's the developer way. Don't be shy or ashamed: Just share your code, your thoughts. Then anybody will understand and try to help the best. Be honest – hc_dev Feb 12 '21 at 20:27
  • Yep You. Are right as it is first time for me so that's why next time there will be a better version of me and thnx for helping me as i think my question will not been answered by anyone all are thinking that i m asking the logic of question – 19UEC089 Mudit Tiwari Feb 13 '21 at 20:23