0% found this document useful (0 votes)
4 views5 pages

Java Remaining 10 Programs

The document provides simple Java programming examples covering various concepts such as interfaces, abstract classes, multilevel inheritance, package imports, file creation, GUI creation with Swing, threading, and servlets. Each example includes concise code snippets demonstrating the implementation of the concepts. The document emphasizes simplicity and clarity in coding practices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Java Remaining 10 Programs

The document provides simple Java programming examples covering various concepts such as interfaces, abstract classes, multilevel inheritance, package imports, file creation, GUI creation with Swing, threading, and servlets. Each example includes concise code snippets demonstrating the implementation of the concepts. The document emphasizes simplicity and clarity in coding practices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ Java Internal – Remaining 10 Programs

Simplest possible code for each question | You got this! ■

Q3. Interface with Single Abstract Method

■ Simple: Define interface with 1 method, implement in class.


interface Greet {
void sayHello(); // abstract method
}

class MyClass implements Greet {


public void sayHello() {
[Link]("Hello from Interface!");
}
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
}
}

Q4. Abstract Class with Abstract + Non-Abstract Methods

■ Simple: Abstract class has 2 methods, subclass fills the abstract one.
abstract class Animal {
abstract void sound(); // abstract
void breathe() {
[Link]("Breathing...");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog says: Woof!");
}
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}

Q5. Multilevel Inheritance

■ Simple: A -> B -> C, each class just adds one print statement.
class A {
void showA() { [Link]("Class A"); }
}
class B extends A {
void showB() { [Link]("Class B"); }
}
class C extends B {
void showC() { [Link]("Class C"); }
public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
[Link]();
}
}

Q6. Access All Classes of a Package using import packageName.*

■ Simple: Just use import [Link].* and use Scanner — no custom package needed.
import [Link].*; // import all classes from [Link] package

class PackageDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
int n = [Link]();
[Link]("You entered: " + n);
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link](list);
}
}

Q8. Create a New File using File Class

■ Simple: Import File, call createNewFile() inside try-catch.


import [Link];
import [Link];

class CreateFile {
public static void main(String[] args) {
try {
File f = new File("[Link]");
if ([Link]()) {
[Link]("File created: " + [Link]());
} else {
[Link]("File already exists.");
}
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
Q9. Swing GUI - Number Buttons (0-9) and Operator Buttons

■ Simplest version: Just add buttons to a JFrame in a grid layout.


import [Link].*;
import [Link].*;

class CalcButtons {
public static void main(String[] args) {
JFrame f = new JFrame("Calculator");
[Link](300, 200);
[Link](new FlowLayout());
String[] labels = {"7","8","9","+",
"4","5","6","-",
"1","2","3","*",
"0","C","=","/"};
for (String s : labels) {
[Link](new JButton(s));
}
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

Q10. Thread by Extending Thread Class - Print 1 to 10

■ Simple: Extend Thread, override run(), print numbers in loop.


class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
[Link]("Number: " + i);
}
}
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}

Q11. Simple Window using JFrame (Swing)

■ Very simple: Create JFrame, set size, make it visible — done!


import [Link].*;

class SimpleWindow {
public static void main(String[] args) {
JFrame frame = new JFrame("My First Window");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
[Link]("Window opened!");
}
}

Q12. Find IP Address of Local Host using InetAddress

■ Simple: Import InetAddress, call getLocalHost() — just 3 lines of logic.


import [Link];

class IPAddress {
public static void main(String[] args) throws Exception {
InetAddress ip = [Link]();
[Link]("Host Name: " + [Link]());
[Link]("IP Address: " + [Link]());
}
}

Q13. Servlet - Display Hello World on Browser

■ Simplest servlet: Just override doGet() and print Hello World to response.
import [Link].*;
import [Link].*;
import [Link].*;

public class HelloServlet extends HttpServlet {


protected void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Hello, World!</h1>");
}
}

■ Remember: Save file as [Link] | Compile: javac [Link] | Run: java ClassName

You might also like