How to write a program
First understand the concept clearly then only start to code, unless your efforts will be of no use.
When you code, you should mean it.
Writing a program is not only about getting the output, you should keep in mind the following things too
1. How efficiently we are using the memory
2. How fast your program runs
What an interviewer or company wants from you
3. Wether your program is clear and easy to understand or not.
I. Use the variable names that relate to their usage
Example : If I want to store student marks
int studentmarks;
int n;
In the above two declarations variable studentmarks is most easy to understand than declaring variable as n
II. Follow indentation while writing the code.
Example
for(int i=0; i<n; i++)
{
System.out.println("value in outer loop is "+i);
for(int j= 0; j< n; j++)
{
System.out.println("value in inner loop is"+j);
}
}
Which statements belongs to which block🤔
Follow indentation
for(int i= 0; i<n; i++)
{
System.out.println("value in outer loop is"+i);
for(int j= 0; j<n; j++)
{
System.out.println("value in inner loop"+i);
} // End of inner for loop
} // End of outer for loop
When we follow indentation program will be very easy to understand. Small things also helps to impress interviewer 🙂
Comments are represented as // comment
When you want give information about a function or set of statements or a single statement we use comment. It will not have any effect on compilation or execution. It is good practice to comment what a function will do when a function is started.
Example
// Function add will receive two integers and display their sum
void add(int a, int b)
{
int sum;
sum= a+b;
System.out.print("sum of a and b is "+sum);
}
When you get an error always try to understand that error read it again. Go the error line try to rectify that.
if not resolved your teacher will always be there for you. 🙂
Good programmers do get lot of errors but they note it understand it and never repeat that error.
So finally
1. Give meaningful names for variables, functions and classes that you define.
2. Follow indentation
3. Comment when needed
4. Learn from errors that you get.
Java Naming conversions
Class Name:
The first letter of java class should be capitalized.
If the class name contains more than two words capitalize first letter of each word.
Class name should not contain any space or special symbols between two words of a class name.
Ex -
Student, StudentMarks are correct
Scanner, System ( classes predefined in java observe naming convention they capitalized first letter of each word in class name)
student, student marks, STUDENT, STUDENTMARKS are not encouraged
Method Name:
If the method name has only one word then all letters should be in lower case.
If the method contains more than one word then except first word capitalize first letter of remaining all the words.
Ex- display(), displayArea(), displayTriangleArea() etc are correct.
println(), sqrt(), nextInt() (methods defined in java, observe naming if method has only one word all letters will be in lowercase. If method has two words then from second word onwards capitalize the first letter like in nextInt() there are two words next and int the second word first letter i.e., I in Int is capitalized.)
Display(), DISPLAY(), DisplayArea() displayarea() are not encouraged.
Classes we came across till now
Scanner class
System class
String class
Math class
Yeah at the begining it will be like WTJ What The Java 🤪. But once you start practicing it be like awesome to program in Java 🤗
Steps for executing a java program.
Consider the Java program to be written is finding area of triangle then steps should be followed
1. Open a text document
( Right on folder select new -> Text document ) and open text document.
2. Select File menu go to save as
Give file name as class Name with .java extension( with in double quotes and java should be in small leters)
Example:
Under file name give as "Area.java"
Below that select File type as All Files and click on Save.
3. The java file will be generated, check file type in file explorer extension should be java. Then java file is ready.
4. With in the file Area.java type the program as needed and save.
5. Click windows and r key together it opens command prompt
6 .type cmd in run prompt and press enter. Command prompt will be opened.
Change the command prompt path to your working directory with following two commands.
C:/users/documents> D: (then press enter)
D:>
D:/>cd SecB1804D403 (then press enter)
D:/SecB1804D403>
As above path will be changed
Using Java's compiler, compile the Area.java as
Ex-
D:/SecB1804D403>javac Area.java
7. The .class file will be generated, if the program has no errors. If there are any errors never open text document open the Area.java file rectify the errors save the file and then compile again using same command prompt(no need to open command prompt again and again use only one command prompt)
8. Using the command prompt run the generated class file as followed
D:/SecB1804D18403> java Area
Note: always open one java file and one command prompt only to avoid confusion.
Edit/ make changes in java file to rectify errors.
Save the changes by pressing control+ s in java file (here Area.java file)
Again compile from command prompt if no errors occur run the class file as
D:/SecB1804D18403> java Area
Output will be obtained.
First understand the concept clearly then only start to code, unless your efforts will be of no use.
When you code, you should mean it.
Writing a program is not only about getting the output, you should keep in mind the following things too
1. How efficiently we are using the memory
2. How fast your program runs
What an interviewer or company wants from you
3. Wether your program is clear and easy to understand or not.
I. Use the variable names that relate to their usage
Example : If I want to store student marks
int studentmarks;
int n;
In the above two declarations variable studentmarks is most easy to understand than declaring variable as n
II. Follow indentation while writing the code.
Example
for(int i=0; i<n; i++)
{
System.out.println("value in outer loop is "+i);
for(int j= 0; j< n; j++)
{
System.out.println("value in inner loop is"+j);
}
}
Which statements belongs to which block🤔
Follow indentation
for(int i= 0; i<n; i++)
{
System.out.println("value in outer loop is"+i);
for(int j= 0; j<n; j++)
{
System.out.println("value in inner loop"+i);
} // End of inner for loop
} // End of outer for loop
When we follow indentation program will be very easy to understand. Small things also helps to impress interviewer 🙂
Comments are represented as // comment
When you want give information about a function or set of statements or a single statement we use comment. It will not have any effect on compilation or execution. It is good practice to comment what a function will do when a function is started.
Example
// Function add will receive two integers and display their sum
void add(int a, int b)
{
int sum;
sum= a+b;
System.out.print("sum of a and b is "+sum);
}
When you get an error always try to understand that error read it again. Go the error line try to rectify that.
if not resolved your teacher will always be there for you. 🙂
Good programmers do get lot of errors but they note it understand it and never repeat that error.
So finally
1. Give meaningful names for variables, functions and classes that you define.
2. Follow indentation
3. Comment when needed
4. Learn from errors that you get.
Java Naming conversions
Class Name:
The first letter of java class should be capitalized.
If the class name contains more than two words capitalize first letter of each word.
Class name should not contain any space or special symbols between two words of a class name.
Ex -
Student, StudentMarks are correct
Scanner, System ( classes predefined in java observe naming convention they capitalized first letter of each word in class name)
student, student marks, STUDENT, STUDENTMARKS are not encouraged
Method Name:
If the method name has only one word then all letters should be in lower case.
If the method contains more than one word then except first word capitalize first letter of remaining all the words.
Ex- display(), displayArea(), displayTriangleArea() etc are correct.
println(), sqrt(), nextInt() (methods defined in java, observe naming if method has only one word all letters will be in lowercase. If method has two words then from second word onwards capitalize the first letter like in nextInt() there are two words next and int the second word first letter i.e., I in Int is capitalized.)
Display(), DISPLAY(), DisplayArea() displayarea() are not encouraged.
Classes we came across till now
Scanner class
System class
String class
Math class
Yeah at the begining it will be like WTJ What The Java 🤪. But once you start practicing it be like awesome to program in Java 🤗
Steps for executing a java program.
Consider the Java program to be written is finding area of triangle then steps should be followed
1. Open a text document
( Right on folder select new -> Text document ) and open text document.
2. Select File menu go to save as
Give file name as class Name with .java extension( with in double quotes and java should be in small leters)
Example:
Under file name give as "Area.java"
Below that select File type as All Files and click on Save.
3. The java file will be generated, check file type in file explorer extension should be java. Then java file is ready.
4. With in the file Area.java type the program as needed and save.
5. Click windows and r key together it opens command prompt
6 .type cmd in run prompt and press enter. Command prompt will be opened.
Change the command prompt path to your working directory with following two commands.
C:/users/documents> D: (then press enter)
D:>
D:/>cd SecB1804D403 (then press enter)
D:/SecB1804D403>
As above path will be changed
Using Java's compiler, compile the Area.java as
Ex-
D:/SecB1804D403>javac Area.java
7. The .class file will be generated, if the program has no errors. If there are any errors never open text document open the Area.java file rectify the errors save the file and then compile again using same command prompt(no need to open command prompt again and again use only one command prompt)
8. Using the command prompt run the generated class file as followed
D:/SecB1804D18403> java Area
Note: always open one java file and one command prompt only to avoid confusion.
Edit/ make changes in java file to rectify errors.
Save the changes by pressing control+ s in java file (here Area.java file)
Again compile from command prompt if no errors occur run the class file as
D:/SecB1804D18403> java Area
Output will be obtained.
Comments
Post a Comment