You are on page 1of 6

Object-oriented programming Lab problems

1a) Write a Java program that reads a line of integers, and then displays each integer, and the
sum of all the integers.

Code:

package labprograms;
import java.util.*;

class Integers_sum {
public static void main(String args[]) {
int n;
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the integers with gap of one space:");
String s = sc.nextLine();
StringTokenizer st = new StringTokenizer(s, " ");
while (st.hasMoreTokens()) {
String temp = st.nextToken();
n = Integer.parseInt(temp);
System.out.println(n);
sum = sum + n;
}
System.out.println("sum of the integers: " + sum);
sc.close();
}
}

Output:
1b) Write a Java program to find both the largest and smallest number in a list of integers.

Code:

package labprograms;
import java.util.Scanner;

public class find_largest_smallest {

private static Scanner input;

public static void main(String[] args) {


int count,items;
int newnum =0 ;
int largest=0;
int smallest=0;

input = new Scanner(System.in);


System.out.println("What is the count of numbers you want to enter?");
items = input.nextInt();
System.out.println("Enter "+items+" numbers: ");

for (count=0; count<items; count++){


newnum = input.nextInt();
if (largest<newnum)
largest=newnum;

if (smallest==0)
smallest=newnum;

else if (newnum<=smallest)
smallest=newnum;
}

System.out.println("The largest number is "+largest);


System.out.println("The smallest number is "+smallest);
}
}

Output:
3) Write a java program that loads names and phone numbers from a text file where the data
is organized as one line per record and each field in a record are separated by tab(\t). It takes
a name or phone number as input and prints the corresponding other value from the hash
table

Code:
package labprograms;
import java.util.*;
import java.io.*;
public class HashTable_Problem {
public static void main(String[] args) {
try {
FileInputStream fs = new FileInputStream("C:\\Users\\sravanthi\\Desktop\\ph.txt.txt");
Scanner sc = new Scanner(fs).useDelimiter("\\s+");
Hashtable<String, String> ht = new Hashtable<String, String>();
String[] arrayList;
String a;
System.out.println("HASH TABLE IS");
System.out.println("--------------------------");
System.out.println("KEY : VALUE");
while (sc.hasNext()) {
a = sc.nextLine();
arrayList = a.split("\\s+");
ht.put(arrayList[0], arrayList[1]);
System.out.println(arrayList[0] + ":" + arrayList[1]);
}
System.out.println("----MENU------");
System.out.println("----1.Search by Name------");
System.out.println("----2.Search by Mobile------");
System.out.println("----3.Exit------");
String opt = "";
String name, mobile;
Scanner s = new Scanner(System.in);
while (opt != "3") {
System.out.println("Enter Your Option 1,2,3");
opt = s.next();
switch (opt) {
case "1": {
System.out.println("Enter the name");
name = s.next();
if (ht.containsKey(name)) {
System.out.println("Mobile is " + ht.get(name));
} else {
System.out.println("Not Found");
}
}
break;
case "2": {
System.out.println("Enter phone number");
mobile = s.next();
if (ht.containsValue(mobile)) {
for (@SuppressWarnings("rawtypes") Map.Entry e : ht.entrySet()) {
if (mobile.equals(e.getValue())) {
System.out.println("Name is " + e.getKey());
}
}
} else {
System.out.println("Not Found");
}
}
break;
case "3": {
opt = "3";
System.out.println("Menu Successfully Exited");
}
break;
default:
System.out.println("Choose Option betwen 1 and 3");
break;
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
File input:

Output:

You might also like