You are on page 1of 15

Practical #01

Q: Write a program that simulates picking a card from a deck of


52 cards. Your program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades) of the card.


public class Exercise3_24 {
public static void main(String[] args) {
final int NUMBER_OF_CARDS = 52;

// Pick a card
int number = (int)(Math.random() * NUMBER_OF_CARDS);

System.out.print("The card you picked is ");
if (number % 13 == 0)
System.out.print("Ace of ");
else if (number % 13 == 10)
System.out.print("Jack of ");
else if (number % 13 == 11)
System.out.print("Queen of ");
else if (number % 13 == 12)
System.out.print("King of ");
else
System.out.print((number % 13) + " of ");

if (number / 13 == 0)
System.out.println("Clubs");
else if (number / 13 == 1)
System.out.println("Diamonds");
else if (number / 13 == 2)
System.out.println("Hearts");
else if (number / 13 == 3)
System.out.println("Spades");
}
}


Output





Practical #02


Q: (Sum a series) Write a program to sum the following series:



public class Exercise4_24 {
public static void main(String[] args) {
double sum = 0;
for (int i = 1; i <= 97; i += 2)
sum += 1.0 * i / (i + 2);

System.out.println("sum is " + sum);
}
}



Output

Sum is 45.124450303050196





Practical #03


Q: Write a method that returns the area of a regular polygon using the following
header:
public static double area(int n, double side)
Write a main method that prompts the user to enter the number of sides and the
side of a regular polygon and displays its area.


import java.util.Scanner;

public class Exercise5_36 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Enter the number of sides
System.out.print("Enter the number of sides: ");
int numberOfSides = input.nextInt();

System.out.print("Enter the side: ");
double side = input.nextDouble();

System.out.println("The area of the polygon is " +
area(numberOfSides, side));
}

public static double area(int n, double side) {
return n * side * side / Math.tan(Math.PI / n) / 4;
}
}

Output






Practical #04


Q: To apply java.util.Arrays.binarySearch(array, key), should the array be
sorted in increasing order, in decreasing order, or neither?

public class Exercise6_26 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);

// Enter values for list1
System.out.print("Enter list1: ");
int size1 = input.nextInt();
int[] list1 = new int[size1];

for (int i = 0; i < list1.length; i++)
list1[i] = input.nextInt();

// Enter values for list2
System.out.print("Enter list2: ");
int size2 = input.nextInt();
int[] list2 = new int[size2];

for (int i = 0; i < list2.length; i++)
list2[i] = input.nextInt();

if (equals(list1, list2)) {
System.out.println("Two lists are strictly identical");
}
else {
System.out.println("Two lists are not strictly identical");
}
}

public static boolean equals(int[] list1, int[] list2) {
if (list1.length != list2.length)
return false;

for (int i = 0; i < list1.length; i++)
if (list1[i] != list2[i])
return false;

return true;
}
}


Output




















Practical #05

Q: Write a test program that prompts the user to enter a 3 * 3 matrix of double values
and displays a new row-sorted matrix.
mport java.util.Scanner;
public class Exercise7_26 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

final int SIZE = 3;
System.out.print("Enter a 3 by 3 matrix row by row: ");
double[][] m = new double[SIZE][SIZE];

for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
m[i][j] = input.nextDouble();

double[][] result = sortRows(m);

for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++)
System.out.print(result[i][j] + " ");
System.out.println();
}
}
public static double[][] sortRows(double[][] m) {
double[][] result = new double[m.length][m[0].length];

// Copy from m to result
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
result[i][j] = m[i][j];

// Check the sum of each column
for (int i = 0; i < result.length; i++)
java.util.Arrays.sort(result[i]);

return result;
}
}
Output

Practical #06

Q: (Display calendars)write the program to display calendars in a message dialog box. Since the
output is generated from several static methods in the class, you may define a static String
variable output for storing the output and display it in a message dialog box.


import javax.swing.JOptionPane;

public class Exercise8_6 {
static String output = "";

/** Main method */
public static void main(String[] args) {
// Prompt the user to enter year
String yearString = JOptionPane.showInputDialog(null,
"Enter full year (i.e. 2001):",
"Enter Year", JOptionPane.QUESTION_MESSAGE);

// Convert string into integer
int year = Integer.parseInt(yearString);

// Prompt the user to enter month
String monthString = JOptionPane.showInputDialog(null,
"Enter month in number between 1 and 12:",
"Enter Month", JOptionPane.QUESTION_MESSAGE);

// Convert string into integer
int month = Integer.parseInt(monthString);

// Print calendar for the month of the year
printMonth(year, month);

JOptionPane.showMessageDialog(null, output);
}

/** Print the calendar for a month in a year */
static void printMonth(int year, int month) {
// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);

// Get number of days in the month
int numOfDaysInMonth = getNumOfDaysInMonth(year, month);

// Print headings
printMonthTitle(year, month);

// Print body
printMonthBody(startDay, numOfDaysInMonth);
}

/** Get the start day of the first day in a month */
static int getStartDay(int year, int month) {
// Get total number of days since 1/1/1800
int startDay1800 = 3;
long totalNumOfDays = getTotalNumOfDays(year, month);

// Return the start day
return (int)((totalNumOfDays + startDay1800) % 7);
}

/** Get the total number of days since Jan 1, 1800 */
static long getTotalNumOfDays(int year, int month) {
long total = 0;

// Get the total days from 1800 to year -1
for (int i = 1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;

// Add days from Jan to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumOfDaysInMonth(year, i);

return total;
}

/** Get the number of days in a month */
static int getNumOfDaysInMonth(int year, int month) {
if (month == 1 || month==3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;

if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;

if (month == 2)
if (isLeapYear(year))
return 29;
else
return 28;

return 0; // If month is incorrect.
}

/** Determine if it is a leap year */
static boolean isLeapYear(int year) {
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;

return false;
}

/** Print month body */
static void printMonthBody(int startDay, int numOfDaysInMonth) {
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
output += " ";

for (i = 1; i <= numOfDaysInMonth; i++) {
if (i < 10)
output += " " + i;
else
output += " " + i;

if ((i + startDay) % 7 == 0)
output += "\n";
}

output += "\n";
}

/** Print the month title, i.e. May, 1999 */
static void printMonthTitle(int year, int month) {
output += " " + getMonthName(month)
+ ", " + year + "\n";
output += "-----------------------------\n";
output += " Sun Mon Tue Wed Thu Fri Sat\n";
}

/** Get the English name for the month */
static String getMonthName(int month) {
String monthName = null;
switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December";
}

return monthName;
}
}






output










Practical #07

Q: (Binary to decimal ) Write a method that parses a binary number as a string into a
decimal integer.

public class Exercise9_8 {
public static void main(String[] args) {
// Prompt the user to enter a string
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a binary number string: ");
String s = input.nextLine();
System.out.println("The decimal value is " + binaryToDecimal(s));
}

public static int binaryToDecimal(String binaryString) {
int value = binaryString.charAt(0) - '0';
for (int i = 1; i < binaryString.length(); i++) {
value = value * 2 + binaryString.charAt(i) - '0';
}

return value;
}
}



Output
Enter a binary number string: 1011
The decimal value is 11





Practical #08

Q:Create an objects of Circle and Rectangle and invokes the methods
on these objects. The toString() method is inherited from the GeometricObject class
and is invoked from a Circle object and a Rectangle object /

public class TestCircleRectangle {
public static void main(String[] args) {
Circle4 circle = new Circle4(1);
System.out.println("A circle " + circle.toString());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());

Rectangle1 rectangle = new Rectangle1(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}



Output







Practical #09

Q:(Game: display a checkerboard ) Write a program that displays a checkerboard in
which each white and black cell is a JButton with a background black or white,
import java.awt.*;
import javax.swing.*;

public class s extends JFrame {
public static void main(String[] args) {
s frame = new s();
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Exercise12_10");
frame.setVisible(true);
}

public s() {
// Create panel p1 add three buttons
JPanel p1 = new JPanel(new GridLayout(8, 8));
add(p1);

JButton[][] buttons = new JButton[8][8];
boolean isWhite = true;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
buttons[i][j] = new JButton();
p1.add(buttons[i][j]);
if (isWhite) {
buttons[i][j].setBackground(Color.WHITE);
isWhite = false;
}
else {
buttons[i][j].setBackground(Color.BLACK);
isWhite = true;
}
}

if (i % 2 == 0)
isWhite = false;
else
isWhite = true;
}
}
}





OUTPUT

You might also like