This is Reservation program full code written in JAVA <from the web>
No Problems are in running process.
I'm required to convert it to C language.
--

1- these are lines that i did not understand it. <could any one explain>
a. private static DataInputStream k = new DataInputStream(System.in);
//what is DataInputStream

b. throws IOException
//what does it do

c. sop
//is it System out print!

d. try {
//what is it

e. Integer.parseInt(k.readLine());
//could any one explain ths line.

f. static void sop(String str) {System.out.print(str);}
g. static void space() {sop("\n\n\n\n\n\n\n\n\n\n\n\n");}
//what is job of these 2 function

2- Can I convert this code to C language? or there are some Lines in this Java code impossible to convert it to C? <Are packages in Java can be converted to C?>
--

import java.io.*;
class Reservation {
	
	private static  DataInputStream k = new DataInputStream(System.in);
	private static  int index=6;
	private static  String resRom[] = new String[index];
	private static  int roomNum;
	private static  String A="Available";
	private static  String R="Reserved";
		
	public  static void main(String[] args)throws IOException {
			roomDis();
			menu();	
	}	
	
	static void roomDis() {
		
		space();
		sop("ROOM #.     STATUS\n\n");
		roomNum=1;
		for(int i=1;i<index;i++) {
			
			resRom[i]=A;
			System.out.println("   " +roomNum + "\t    " +resRom[i]);
			roomNum++;
		}
		
	}
	
	static void roomDisOrg() {
		
		space();
		sop("ROOM #.     STATUS\n\n");
		roomNum=1;
		for(int i=1;i<index;i++) {
			
			System.out.println("   " +roomNum + "\t    " +resRom[i]);
			roomNum++;
		}
			
	}
	
	static void menu()throws IOException {
	
	try {
	
		
		sop("\n\nMenu \n [1] - Reserve \n [2] - Cancel Reservation \n [3] - Exit");	
		sop("\n\n\nEnter Menu: ");
		int menu = Integer.parseInt(k.readLine());
		
		switch(menu) {
			case 1:
			
					System.out.print("Enter the room #: ");
					int rNo = Integer.parseInt(k.readLine());
					
						if(resRom[rNo]==A) {
							
							resRom[rNo]=R;
							roomDisOrg();
							System.out.println("\n\nRoom number " + rNo + " reserved!");
							menu();
							
						}else {
						   space();
						   roomDisOrg();
						   System.out.println("\n\nRoom number " + rNo + " already reserved!");
						   menu();
						}	
					
					
						
				break;
			case 2:
			
				
					System.out.print("Enter the room #: ");
					int rNo1 = Integer.parseInt(k.readLine());
				
					
					System.out.print("\nCancel this room?[1]-Yes/[2]-No: ");
					int ask = Integer.parseInt(k.readLine());
					
						if(ask==1) {
									
							if(resRom[rNo1]==R) {
									
							   space();
							   resRom[rNo1] = A;
							   roomDisOrg();
							   System.out.println("\n\nRoom number " + rNo1 + " is now " + A);
							   menu();
							 
							}else {
								
							   space();
							   roomDisOrg();
							   System.out.println("\n\nRoom number " + rNo1 + " is already " + A);
							   menu();
								
							}
								
						}else if(ask==2) {
							
							space();
							roomDisOrg();
							menu();
													
						}else {
							
							sop("Invalid Input...\n");	
								
						}		
						
				break;
			case 3:
				
				System.exit(0);
				break;
			
			default:
			
				space();
				roomDisOrg();
				System.out.println("\n\nInvalid menu!");
				menu();
				break;
		}
		
		}catch(NumberFormatException er) {
			
				space();
				roomDisOrg();
				System.out.println("\n\nInvalid menu!");
				menu();
				
		}
		
	}
	
	static void sop(String str) {
		System.out.print(str);
	}
	
	static void space() {
		sop("\n\n\n\n\n\n\n\n\n\n\n\n");
	}
	
}

HELP please.
& Thanks

Recommended Answers

All 2 Replies

Before going further, I need to clarify something. The word method in Java is the same as function in C or C++.

1- these are lines that i did not understand it. <could any one explain>
a. private static DataInputStream k = new DataInputStream(System.in);
//what is DataInputStream

The DataInputStream is just a class that deals with reading data in. I think in C works as an object that can fopen() and fclose() without explicitly call these function.

b. throws IOException
//what does it do

It is an error handling. In Java, you use Exception when an unexpected error due to user input or the program itself occurs and interrupts your program. This throw will actually move the program to a stage where you can handle the way you want. There is no such a thing in C and you may get Segmentation Fault instead.

c. sop
//is it System out print!

Apparently it is just a print without new line at the end. It is a very silly method which should not even be there. Don't implement it! Also, the same goes to space() method...

d. try {
//what is it

In Java, you can create a block which will catch if there is any error occurs. The error is called Exception. It is OO programming which is a bit different from C but is still in the same sense. Though, there is no Exception in C because you have to handle everything yourself manually. If you handle it incorrectly, you would get Segmentation Fault in C.

e. Integer.parseInt(k.readLine());
//could any one explain ths line.

You see the k.readLine()? It is similar to getLine() that read a line of string from an input. In this code, the k is reading from keyboard input (System.in). The Integer.parseInt() method is a method that read in a String and return an integer value. In other words, it is similar to atoi().

f. static void sop(String str) {System.out.print(str);}

As stated above, this is just a silly print out with out entailing line break. It is similar to print() in C (or printf()?), if I remember correctly.

g. static void space() {sop("\n\n\n\n\n\n\n\n\n\n\n\n");}
//what is job of these 2 function

As state above, it is a silly method. You could simply call the println with string of newline in it. The person who codes this is still learning.

2- Can I convert this code to C language? or there are some Lines in this Java code impossible to convert it to C? <Are packages in Java can be converted to C?>

Yes, you can convert any language to another. Sometimes you could find a substitute library that works the same way. Sometimes, you cannot find any and you must implement it yourself. In this case, there is no special package you need. If you use just standard library from C, you should be able to do it.

commented: 1 +1

Thank you very much Taywin, every thing became clear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.