WT File
WT File
i. If-Else Statements
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
}
}
Output:
// Base class
class Bicycle {
// Fields
public int gear;
public int speed;
// Constructor
public Bicycle(int gear, int speed) {
this.gear = gear;
this.speed = speed;
}
// Methods
public void applyBrake(int decrement) {
speed -= decrement;
}
// Derived class
class MountainBike extends Bicycle {
// Additional field
public int seatHeight;
// Method
public void setHeight(int newValue) {
seatHeight = newValue;
}
// Driver class
public class Test {
public static void main(String args[]) {
MountainBike mb = new MountainBike(3, 100, 25);
System.out.println(mb.toString());
}
}
Output:
interface Character {
void attack();
}
interface Weapon {
void use();
}
ii. Throw
class tst {
public static void main(String[] args) throws InterruptedException {
Thread.sleep(10000);
System.out.println("Hello Professor :D");
}
}
Output:
// Greeting message
System.out.println("Hello, " + userName + "! Welcome to the program.");
}
}
Output:
// Printing String
System.out.println("Entered String : " + str);
// Printing Integer
System.out.println("Entered Integer : " + it);
}
}
Output:
Java Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
<label>Middlename:</label>
<input type="text" name="middlename" size="15"/> <br> <br>
<label>Lastname:</label>
<input type="text" name="lastname" size="15"/> <br> <br>
<label>Course:</label>
<select>
<option value="Course">Course</option>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option>
<option value="B. Tech">B. Tech</option>
<label>Gender:</label><br>
<input type="radio" name="gender" value="male"/> Male <br>
<input type="radio" name="gender" value="female"/> Female <br>
<input type="radio" name="gender" value="other"/> Other
<br>
<br>
<label>Phone:</label>
<input type="text" name="country code" value="+91" size="2"/>
<input type="text" name="phone" size="10"/> <br> <br>
Address:<br>
<textarea cols="80" rows="5" name="address"></textarea>
<br>
<br>
Email:<br>
<input type="email" id="email" name="email"/> <br>
<br>
Password:<br>
<input type="password" id="pass" name="pass"> <br>
<br>
Re-type password:<br>
<input type="password" id="repass" name="repass"> <br>
try {
// Registering drivers
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
// Creating a connection
// Creating a statement
Statement st = con.createStatement();
// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println("Inserted successfully: " + sql);
else
System.out.println("Insertion failed");
} catch (Exception ex) {
// Display message when exceptions occur
System.err.println(ex);
} finally {
try {
// Closing the connections
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Output:
try {
// Registering drivers
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Creating a connection
con = DriverManager.getConnection(url, user, pass);
// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println("Inserted successfully: " + sql);
else
System.out.println("Insertion failed");
} catch (Exception ex) {
// Display message when exceptions occur
System.err.println(ex);
} finally {
try {
// Closing the connections
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Output: