- Write a program using a "Stack" object that allows a user to type in
numbers until the value -1 is seen, after which the numbers are displayed back
in reverse order
import java.io.*;
public class Reverse
{
public static void main(String[] args)
{
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
java.util.Stack sp = new java.util.Stack();
int answer = 0;
do {
System.out.print("Number ? ");
try {
String val = stdin.readLine();
answer = Integer.parseInt(val);
if (answer != -1) {
sp.push(new Integer(answer));
}
}
catch (Exception e) {
// Any error, assume end of list
answer = -1;
}
}
while (answer != -1);
System.out.print("In reverse order, the numbers are : ");
while (!(sp.empty())) {
Integer i = (Integer)sp.pop();
System.out.print(i + " ");
}
System.out.println();
}
}
- Write a program that will convert values from Hex to Decimal and vice
versa.
public class Convert {
public static void main(String[] args)
{
try {
String val = args[0].trim();
if (val.startsWith("0x")) {
// Assume it's hex
int intVal = Integer.parseInt(val.substring(2),16);
System.out.println(val + " == " + intVal);
}
else {
// Assume it's decimal
Integer x = new Integer(val);
System.out.println(val + " == 0x"
+ Integer.toHexString(x.intValue()));
}
}
catch (Exception e) {
System.out.println("Please give a meaningful argument");
}
}
}
- Write a program that will multiply two arbitrarily large integers
import java.math.*;
public class Multiply
{
public static void main(String[] args)
{
BigInteger b1, b2;
try {
b1 = new BigInteger(args[0]);
b2 = new BigInteger(args[1]);
}
catch (Exception e) {
System.out.println("Usage : java Multiply [int_1] [int_2]");
return;
}
BigInteger result = b1.multiply(b2);
System.out.println(b1 + " * " + b2 + " = " + result);
}
}
- Write a program that will tell you what day of the week your birthday is
import java.util.*;
import java.text.*;
public class Birthday
{
public static void main(String[] args)
{
// This will hold our birthday
GregorianCalendar birthday = new GregorianCalendar();
// Start off with today's date, so we've got the current year
birthday.setTime(new Date());
// Set the day and month from the caller's arguments
birthday.set(Calendar.DAY_OF_MONTH, Integer.parseInt(args[0]));
// Months are numbered 0-11 so far as Calendar is concerned
int calendarMonth = (Integer.parseInt(args[1])) - 1;
birthday.set(Calendar.MONTH, calendarMonth);
// Display the day name using "SimpleDateFormat"
// "E" means day of week, make it more than 4 characters to get
// the long form of the name
final SimpleDateFormat sdf = new SimpleDateFormat("EEEEE");
System.out.println("This year your birthday is on a "
+ sdf.format(birthday.getTime()));
}
}
- Write a program that lists the files in a zipfile
import java.util.zip.*;
public class ListZipContents
{
public static void main(String[] args)
{
try {
ZipFile z = new ZipFile(args[0]);
java.util.Enumeration e = z.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry)e.nextElement();
System.out.println(ze);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
- Write a program that will extract the host name and path from a www
address
import java.net.*;
public class WWW
{
public static void main(String [] args)
{
try {
URL url = new URL(args[0]);
System.out.println("host is " + url.getHost());
System.out.println("path is " + url.getPath());
}
catch (Exception e) {
System.out.println(e);
}
}
}
- Write a Java program that accepts an IP address on the command line and
translates that address into a proper nodename
import java.net.*;
class NodeInfo
{
public static void main(String[] args)
{
try {
InetAddress i = InetAddress.getByName(args[0]);
System.out.println(args[0] + " : " + i.getHostName() + "/"
+ i.getHostAddress());
}
catch (Exception e) {
System.out.println(e);
}
}
}