Oop Lab Assignment 1
Oop Lab Assignment 1
ASSIGNMENT NO: 01 :-
OBJECT ORIENTED
PROGRAMMING (LAB) :-
BACHELORS IN
COMPUTER SCIENCE (BSCS) :-
CHAPTER # 03
PROGRAM : (AverageScore.java)
import javax.swing.JOptionPane; // Needed for JOptionPane
/**
*/
score1 = Double.parseDouble(input);
score2 = Double.parseDouble(input);
score3 = Double.parseDouble(input);
// If the score was greater than 95, let the user know
JOptionPane.showMessageDialog(null,
System.exit(0);
}
PROGRAM 3.1:-(LoanQualifier.java)
import javax.swing.JOptionPane; // Needed for JOptionPane class
/**
*/
"annual salary.");
salary = Double.parseDouble(input);
yearsOnJob = Double.parseDouble(input);
if (yearsOnJob >= 2)
}
else
else
System.exit(0);
/**
*/
testScore = Integer.parseInt(input);
else
else
else
else
{
System.exit(0);
}
PROGRAM 3.3(TestResults.Java)
import javax.swing.JOptionPane; // Needed for JOptionPane
testScore = Integer.parseInt(input);
else
System.exit(0);
}
}
PROGRAM 3.4(TrailingElse.java)
import javax.swing.JOptionPane; // Needed for JOptionPane
/**
*/
testScore = Integer.parseInt(input);
else
System.exit(0);
PROGRAM 3.5(LogicalAnd.java)
import javax.swing.JOptionPane; // Needed for JOptionPane class
/**
*/
"annual salary.");
salary = Double.parseDouble(input);
else
System.exit(0);
PROGRAM 3.6(LOGICAL.OPERATOR)
import javax.swing.JOptionPane; // Needed for JOptionPane class
/**
*/
"annual salary.");
salary = Double.parseDouble(input);
yearsOnJob = Double.parseDouble(input);
else
System.exit(0);
/**
*/
public class CurrencyFormat
JOptionPane.showMessageDialog(null,
PROGRAM 3.8(CurrencyFormat2.java)
import javax.swing.JOptionPane;
/**
*/
JOptionPane.showMessageDialog(null, output);
*/
// in a field of 8 spaces.
PROGRAM 3.10(LeadingZeros.java)
/**
*/
{
public static void main(String[] args)
// to 2 decimal places.
System.out.printf("%09.2f\n", number1);
System.out.printf("%09.2f\n", number2);
System.out.printf("%09.2f\n", number3);
PROGRAM 3.11(CurrencyFormat.java)
/**
*/
PROGRAM 3.12(Columns.java)
/**
*/
System.out.printf("%8.2f\n", num1);
System.out.printf("%8.2f\n", num2);
System.out.printf("%8.2f\n", num3);
System.out.printf("%8.2f\n", num4);
System.out.printf("%8.2f\n", num5);
System.out.printf("%8.2f\n", num6);
}
}
PROGRAM 3.13(StringCompare.java)
/**
*/
name2 = "Mark",
name3 = "Mary";
if (name1.equals(name2))
else
if (name1.equals(name3))
{
else
}
(StringCompareTo.java)
/**
*/
name2 = "Mark";
if (name1.compareTo(name2) < 0)
else if (name1.compareTo(name2) == 0)
}
else if (name1.compareTo(name2) > 0)
PROGRAM 3.14(VariableScope.java)
/**
*/
String firstName;
"first name.");
String lastName;
"last name.");
System.exit(0);
PROGRAM 3.15(Columns.java)
/**
*/
System.out.printf("%8.2f\n", num1);
System.out.printf("%8.2f\n", num2);
System.out.printf("%8.2f\n", num3);
System.out.printf("%8.2f\n", num4);
System.out.printf("%8.2f\n", num5);
System.out.printf("%8.2f\n", num6);
}
CHAPTER # 04
PROGRAM :4..1 (IncrementDecrement.java)
/**
*/
// Increment number.
number++;
// Decrement number.
number--;
in prefix mode.
*/
// Increment number.
++number;
// Decrement number.
--number;
PROGRAM4-3 (WhileLoop.java)
/**
int number = 1;
System.out.println("Hello");
number++;
System.out.println("That's all!");
}
PROGRAM 4.4 (TestAverage1.java)
import java.util.Scanner; // Needed for the Scanner class
/**
*/
do
score1 = keyboard.nextInt();
score2 = keyboard.nextInt();
boolean
// Get the third test score in this set.
score3 = keyboard.nextInt();
keyboard.nextLine();
3 */
6{
8{
9 int number; // Loop control variable
10
12 System.out.println("------------------------");
13
15 {
16 System.out.println(number + "\t\t" +
17 number * number);
18 }
19 }
20 }
PROGRAM 4.6(UserSquares.java)
1 import java.util.Scanner; // Needed for the Scanner class
3 /**
5 */
8{
10 {
13
19
22 maxValue = keyboard.nextInt();
23
26 System.out.println("-----------------------");
28 {
29 System.out.println(number + "\t\t" +
30 number * number);
31 }
32 }
4 */
7{
9{
10 // Constants
11 final int STARTING_KPH = 60; // Starting speed
14
15 // Variables
18
20 System.out.println("KPH\t\tMPH");
21 System.out.println("-------------------");
22
25 {
28
31 }
32 }
33 }
PROGRAM 4.8(TotalSales.java)
1 import javax.swing.JOptionPane;
3 /**
8{
10 {
15
19 days = Integer.parseInt(input);
20
22 totalSales = 0.0;
23
26 {
29 sales = Double.parseDouble(input);
31 }
32
33 // Display the total sales.
34 JOptionPane.showMessageDialog(null,
36
37 System.exit(0);
38 }
39 }
Program 4.9(SoccerPoints.java)
1 import java.util.Scanner; // Needed for the Scanner class
3 /**
7 */
10 {
12 {
15
18
23 System.out.println();
24
27 points = keyboard.nextInt();
28
31 {
33 totalPoints += points;
34
37 points = keyboard.nextInt();
38 }
39
42 totalPoints);
(PROGRAM 4.10Clock.java)
1 /**
3 */
4
5 public class Clock
6{
8{
11 {
13 {
15 {
17 }
18 }
19 }
20 }
21 }
PROGRAM 4.11(RectangularPattern.java)
1 import java.util.Scanner;
3 /**
5 of asterisks.
6 */
9{
10 public static void main(String[] args)
11 {
13
16
19 rows = keyboard.nextInt();
21 cols = keyboard.nextInt();
22
24 {
26 {
27 System.out.print("*");
28 }
29 System.out.println();
30 }
31 }
32 }
PROGRAM 4.12(FileWriteDemo.java)
1 import java.util.Scanner; // Needed for Scanner class
4 /**
6 */
9{
11 {
15
18
21 numFriends = keyboard.nextInt();
22
24 keyboard.nextLine();
25
28 filename = keyboard.nextLine();
29
35 {
39 friendName = keyboard.nextLine();
40
42 outputFile.println(friendName);
43 }
44
46 outputFile.close();
48 }
49 }
2 import java.util.Random;
4 /**
6 */
9{
10 public static void main(String[] args)
11 {
15
18
21
23 while (again.equalsIgnoreCase("y"))
24 {
26 die1 = rand.nextInt(6) + 1;
27 die2 = rand.nextInt(6) + 1;
30
32 again = keyboard.nextLine();
33 }
34 }
35 }
PROGRAM 4.15(CoinToss.java)
1 import java.util.Random;
2
3 /**
5 */
8{
10 {
13
16 {
17 if (rand.nextInt(2) == 0)
18 System.out.println("Tails");
19 else
20 System.out.println("Heads");
21 }
22 }
23 }
PATTERN PRINTING PROGRAMS :-
1. Right Triangle Star Pattern
Output:
Output:
import java.util.Scanner;
public class DiamondPattern
{
public static void main(String args[])
{
int row, i, j, space = 1;
System.out.print("Enter the number of rows you want
to print: ");
Scanner sc = new Scanner(System.in);
row = sc.nextInt();
space = row - 1;
for (j = 1; j<= row; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j<= row - 1; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i<= 2 * (row - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
Output: