2. Java Syntax Understand these things: (a) Know how to write the C-style contro
ID: 3595363 • Letter: 2
Question
2. Java Syntax Understand these things:
(a) Know how to write the C-style control structures: for loop, do loop, while loop, if...break, if...else, switch.
(b) Know how to write a for-each loop.
(c) Know how to write a class denition, with constructor, toString(), and accessors.
(d) Know how to instantiate and use and ArrayList.
(e) Know how to write the main function.
(f) Understand what belongs in the main function and what does not belong there.
(g) Explain try, catch, throw, throws, nally, and getMessage(). How are these things used?
Questions from java programming course,Answer for above questions in basis of java syntax.
Explanation / Answer
for(initialization statements; condition; increment/decrement statements){
action statements;
…. Statements….
}
do{
action statements;
}while(condition);
while (condition) {
action statements:
}
if(condition 1){
}
if(condition 1){
action 1;
}else if(condition 2){
action 2;
}else{
action 3;
}
switch(expression){
case 1 :
action1 statement;
break;
case 2:
action2 statement;
break;
.
.
.
case N:
actionN statement;
break;
default:
default statement;
}
for(data_type variable : array | collection){
------
}
public class School {
private String schoolName;
private boolean isPublicSchool;
public School(String schlName) {
this.schoolName = schlName;
this.isPublicSchool = false;
}
School(String schlName, boolean isPubSchool) {
this.schoolName = schlName;
this.isPublicSchool = isPubSchool;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public boolean isPublicSchool() {
return isPublicSchool;
}
public void setPublicSchool(boolean isPublicSchool) {
this.isPublicSchool = isPublicSchool;
}
public String toString() {
return "Name: " + this.schoolName + " Public: " + this.isPublicSchool;
}
}
list.add(e);
public static void main(String[] args) {
// statement to be executed
}
Main function must not have any kind of class declaration including in it.
A catch block is where you handle the exceptions, this block must follow the try block. A single try block can have several catch blocks associated with it. You can catch different exceptions in different catch blocks. When an exception occurs in try block, the corresponding catch block that handles that particular exception executes. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.
Syntax
try
{
//statements that may cause an exception
}
catch (exception(type) e(object))
{
//error handling code
}
Throw : - The throw keyword in Java is used to explicitly throw an exception from a method or any block of code
Syntax: - throw Instance
Eg throw new Exception();
Throws:- throws is a keyword in Java which is used in the signature of method to indicate that this method might throw one of the listed type exceptions. The caller to these methods has to handle the exception using a try-catch block.
Finally:- Java finally block is a block that is used to execute important code such as closing connection, stream etc. Java finally block is always executed whether exception is handled or not. Java finally block follows try or catch block.
Eg.
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
getMessage:- The java.lang.Throwable.getMessage() method returns the detail message string of this throwable.
Eg: -
try {
newException();
} catch(Throwable e) {
System.err.println(e);
// returns the detail message string of this Throwable instance
System.out.println(e.getMessage());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.