Strings
Strings
Strings
1)
Strings
Code:
// Extracting 'park'
ii) Write a program that inputs from the user a file pathname and extracts: path, filename and extension.
Path: \programs\java
Filename: testProgram
Extension: java
Code:
import java.util.Scanner;
Code:
import java.util.Scanner;
if (isPalindrome(str)) {
System.out.println(str + " is a palindrome.");
} else {
str = str.toLowerCase();
int left = 0;
if (str.charAt(left) != str.charAt(right)) {
return false;
left++;
right--;
return true;
iv) Write and test a program having a method newString () that takes two string objects as arguments. The string compares (lexicographically) the two string arguments
and returns the concatenation of the arguments with the lesser string coming first.
Code:
import java.util.Scanner;
if (str1.compareTo(str2) <= 0) {
return str1.concat(str2);
} else {
return str2.concat(str1);
v) Write a program with a method strInsert() that takes str1 and str2 and an integer pos, as arguments. The method inserts str2 in str1 beginning at position pos
provided that pos < str1.length().Otherwise str2 is inserted at the end of str1.Implement the methods using substring and ‘+’ methods.
Code:
import java.util.Scanner;
} else {
}
}
vi) A method strReplace takes strings str1, str2 and str3 as arguments. In str1, the method replaces all occurrences of str2 with str3. Implement using appropriate String
class methods. In the main method as ask for input from the keyboard using an input dialog and display the resulting string using a message dialog.
Code:
import javax.swing.JOptionPane;
}
2)
File input and output
th
1. Exercise 15.4, Deitel and Deitel , 10 Edition.
Code:
this.accountNumber = accountNumber;
this.amount = amount;
return accountNumber;
this.accountNumber = accountNumber;
return amount;
}
this.amount = amount;
Code:
this.accountNumber = accountNumber;
this.name = name;
this.balance = balance;
return accountNumber;
this.accountNumber = accountNumber;
}
public String getName() {
return name;
this.name = name;
return balance;
this.balance = balance;
if (amount > 0) {
this.balance += amount;
} else {
this.balance -= absAmount;
} else {
}
}
3)
Generics and Collections
a) Explain with aid of code snippets the use of the keywords super, extends and implements for java Generics.
Extends
The extends keyword is used to specify an upper bound for a type parameter. It restricts the type argument to be a subtype of the specified type. The syntax for using extends is as
follows:
// class implementation
In the example above, T is a type parameter that is bounded by SomeClass. This means that any type argument passed to MyClass must be a subtype of SomeClass.
Super
The super keyword is used to specify a lower bound for a type parameter. It restricts the type argument to be a supertype of the specified type. The syntax for using super is as
follows:
// class implementation
In the example above, T is a type parameter that is bounded by SomeClass. This means that any type argument passed to MyClass must be a supertype of SomeClass.
Implements
The implements keyword is used to specify that a class or interface implements one or more interfaces. The syntax for using implements is as follows:
// class implementation
In the example above, MyClass implements AnotherInterface and has a type parameter T that is bounded by SomeInterface. This means that any type argument passed to MyClass
i) An initial list of 50 students each taking 8 units. A student is an object with a name and registration number.
iii) Traverse the entire list using ListIterator and display student information in a tabular format
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
studentList.add(student);
studentList.remove(0);
// Traverse the entire list using ListIterator and display student information in a tabular format
System.out.println("Name\tRegistration No.\tCourses");
while (iterator.hasNext()) {
class Student {
this.name = name;
this.regNo = regNo;
return name;
}
public String getRegNo() {
return regNo;
return courses;
courses.add(course);
4)
Databases
1. With aid of an example explain the need for a ResultSetMetaData object in JDBC
In JDBC, a ResultSetMetaData object is used to retrieve metadata (information about the data) from a ResultSet object. This metadata includes information such as the number and
types of columns in the result set, the column names, and the column labels.
2. Create a data source, books db, based on the SQL code provided in the following link : http://www2.cs.uregina.ca/~nova/Java_Study/Examples8/books.sql
Use the database created above to create a simple jdbc-GUI-based application that displays the following on the screen :
c) A specific author and list all books for that author. Include the title, year and ISBN. Put the information in alphabetical order by the title.
d) A specific publisher and list all books published by the publisher. Include the title, year, and ISBN. Put the information in alphabetical order by title.
i) Socket
Socket class represents a client-side endpoint that provides a connection to a server-side endpoint represented by a ServerSocket. It is used to establish a two-way communication
link between a client and a server over a network. Here's an example of how to use Socket to connect to a server:
} catch (IOException e) {
// handle exception
ii) ServerSocket
ServerSocket class represents a server-side endpoint that listens for incoming client connections and creates a Socket object for each new connection. Here's an example of how to
while (true) {
} catch (IOException e) {
// handle exception
iii) InetAddress
InetAddress class represents an IP address, either IPv4 or IPv6. It provides methods to resolve host names to IP addresses and vice versa. Here's an example of how to use
System.out.println(address.getHostAddress());
} catch (UnknownHostException e) {
// handle exception
iv) Inet4Address
Inet4Address class represents an IPv4 address specifically. It is a subclass of InetAddress and provides additional methods to work with IPv4 addresses. Here's an example of how to
try {
System.out.println(address.getHostAddress());
} catch (UnknownHostException e) {
// handle exception
v) DatagramSocket
DatagramSocket class represents a socket for sending and receiving datagrams, which are small packets of data. It provides methods for sending and receiving datagrams over a
socket.send(packet);
} catch (IOException e) {
// handle exception
vi) DatagramPacket
It holds message data.
socket.send(packet);
} catch (IOException e) {
// handle exception
b) Write complete programs(client and server side) to demonstrate socket programming using the following socket APIs:
i) TCP
ii) UDP
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
try {
while (true) {
out.println("Hello, client!");
} catch (IOException e) {
e.printStackTrace();
Client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
try {
out.println("Hello, server!");
socket.close();
} catch (IOException e) {
e.printStackTrace();
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
try {
socket.receive(packet);
buffer = message.getBytes();
socket.send(packet);
socket.close();
} catch (IOException e) {
e.printStackTrace();
Client
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient {
try {
socket.send(packet);
socket.receive(packet);