0% found this document useful (0 votes)
3 views

JAVA notes

The document provides an overview of various programming concepts in Java, including operators (arithmetic, relational, logical, bitwise, assignment, increment/decrement), control structures (selection and iteration), and practical examples such as the Rabbit and Chicken problem, a food order program, and a bookstore management system. It also covers class creation, object instantiation, and methods for calculating averages and displaying details. The content is structured to serve as a guide for beginners in understanding Java programming fundamentals.

Uploaded by

dummyy0223
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA notes

The document provides an overview of various programming concepts in Java, including operators (arithmetic, relational, logical, bitwise, assignment, increment/decrement), control structures (selection and iteration), and practical examples such as the Rabbit and Chicken problem, a food order program, and a bookstore management system. It also covers class creation, object instantiation, and methods for calculating averages and displaying details. The content is structured to serve as a guide for beginners in understanding Java programming fundamentals.

Uploaded by

dummyy0223
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

1.

OUTLINE OPERATORS
1. Arithmetic Operators
* + (Addition): Adds two operands.
* int result = 5 + 3; // result will be 8
* - (Subtraction): Subtracts the second operand from the first.
* int result = 10 - 4; // result will be 6
* * (Multiplication): Multiplies two operands.
* int result = 2 * 6; // result will be 12
* / (Division): Divides the first operand by the second.
* int result = 15 / 3; // result will be 5 (integer division)
* double result = 15.0 / 3; // result will be 5.0 (double division)
* % (Modulus): Returns the remainder after division.
* int result = 17 % 5; // result will be 2
2. Relational Operators
* > (Greater Than): Checks if the left operand is greater than the right.
* boolean result = 10 > 5; // result will be true
* < (Less Than): Checks if the left operand is less than the right.
* boolean result = 3 < 8; // result will be true
* >= (Greater Than or Equal To): Checks if the left operand is greater
than or equal to the right.
* boolean result = 7 >= 7; // result will be true
* <= (Less Than or Equal To): Checks if the left operand is less than or equal
to the right.
* boolean result = 2 <= 4; // result will be true
* == (Equal To): Checks if both operands are equal.
* boolean result = 5 == 5; // result will be true
* != (Not Equal To): Checks if both operands are not equal.
* boolean result = 10 != 7; // result will be true
3. Logical Operators
* && (Logical AND): Returns true if both operands are true.
* boolean result = (5 > 3) && (2 < 8); // result will be true
* || (Logical OR): Returns true if at least one operand is true.
* boolean result = (10 < 5) || (2 > 1); // result will be true
* ! (Logical NOT): Reverses the logical state of its operand.
* boolean result = !(5 > 3); // result will be false
4. Bitwise Operators (operate on bits of integers)
* & (Bitwise AND)
* | (Bitwise OR)
*^ (Bitwise XOR)
* ~ (Bitwise NOT)
* << (Left Shift)
* >> (Right Shift)
* >>> (Unsigned Right Shift)
5. Assignment Operators
* = (Simple Assignment): Assigns the value of the right operand to the left
operand.
* int x = 10;
* += (Addition Assignment): Adds the right operand to the left
operand and assigns the result to the left operand.
* x += 5; // equivalent to x = x + 5;

* -= (Subtraction Assignment): Subtracts the right operand from


the left operand and assigns the result to the left operand.
* *= (Multiplication Assignment): Multiplies the left operand with
the right operand and assigns the result to the left operand.
* /= (Division Assignment): Divides the left operand by the right
operand and assigns the result to the left operand.
* %= (Modulus Assignment): Performs modulus operation on the left
and right operands and assigns the result to the left operand.
6. Increment/Decrement Operators
* ++ (Increment): Increases the value of the operand by 1.
* int x = 5;
* int y = ++x; // x becomes 6, y becomes 6 (pre-increment)
* int z = x++; // z becomes 6, x becomes 7 (post-increment)
* -- (Decrement): Decreases the value of the operand by 1.
Example Program
public class OperatorsExample {
public static void
main(String[] args) { int a
= 10;
int b = 5;

System.out.println("a + b = " +
(a + b)); System.out.println("a
- b = " + (a - b));
System.out.println("a * b = " +
(a * b)); System.out.println("a
/ b = " + (a / b));
System.out.println("a % b = "
+ (a % b));

boolean isGreater = a > b;


System.out.println("a > b: " +
isGreater);

boolean isTrue = true && false;


System.out.println("true && false:
" + isTrue);

a += 3;
System.out.println("a += 3: " + a);

int x =
5; int y
= ++x;
System.out.println("++x: " + x + ", y: " + y);
}
}
2.SELECTION AND ITERATION CONTROL STRUCTURE
1. Selection Control Structures
* if Statement: This structure executes a block of code only if a
specific condition is true. int age = 25;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}

* if-else Statement: This structure executes one block of code if a


condition is true and another block if the condition is false.
int number = 10;
if (number % 2 == 0) {
System.out.println("The number
is even.");
} else {
System.out.println("The number is odd.");
}

* switch Statement: This structure evaluates an expression and


executes the code block associated with the matching case.
int dayOfWeek =
3; switch
(dayOfWeek) {
case 1:
System.out.println("Mo
nday"); break;
case 2:
System.out.println("Tuesday
"); break;
case 3:
System.out.println("Wednes
day"); break;
default:
System.out.println("Invalid day");
}

2. Iteration Control Structures


* for Loop: This structure executes a block of code a specific
number of times. for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}

* while Loop: This structure executes a block of code as long as a


given condition remains true.
int count = 0;
while (count <
3) {
System.out.println("Count: "
+ count); count++;
}

* do-while Loop: This structure executes a block of code at least once


and then continues to execute as long as a given condition remains true.
int i =
0; do {
System.out.println("i:
" + i); i++;
} while (i < 5);

Key Concepts
* Control Flow: The order in which statements are executed in a program.
* Condition: An expression that evaluates to true or false.
* Iteration: Repeated execution of a
block of code. Explanation
* Selection: Allows you to choose which block of code to execute based on a
condition.
* Iteration: Enables you to repeat a set of instructions multiple times.
These control structures are fundamental to programming in Java and
many other languages.
3. RABBIT AND CHICKEN
PROGRAM
public class
RabbitChickenProblem {
public static void
main(String[] args) {
int totalHeads = 35; // Replace with the actual number of heads
int totalLegs = 94; // Replace with the actual

number of legs int rabbitCount =

findRabbitCount(totalHeads, totalLegs);

if (rabbitCount != -1) {

int chickenCount = totalHeads - rabbitCount;


System.out.println("Number of rabbits: " +
rabbitCount); System.out.println("Number of chickens: "
+ chickenCount);
} else {
System.out.println("No valid solution found.");
}
}

private static int findRabbitCount(int totalHeads,


int totalLegs) { for (int rabbits = 0; rabbits <=
totalHeads; rabbits++) {
int chickens = totalHeads - rabbits;
int totalLegsCalculated = (rabbits * 4) + (chickens * 2);

if (totalLegsCalculated ==
totalLegs) { return rabbits;
}
}
return -1; // No valid solution found
} }
4.(i) java program gift vocher and 150
Import

java.util.Scanner;

public class

FoodOrder {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of food
items ordered: "); int numberOfItems =
scanner.nextInt();

System.out.print("Enter customer type


(Regular/Guest): "); String customerType =
scanner.next();

double totalCost = numberOfItems * 150.0;

if
(customerType.equalsIgnoreCase("Re
gular")) { totalCost *= 0.95; // Apply
5% discount
if (totalCost > 300) {
System.out.println("Congratulations! You have received a special gift
voucher.");
}
} else if
(customerType.equalsIgnoreCase("Guest"))
{ totalCost += 5.0; // Add delivery charge
} else {
System.out.println("Invalid customer type. Please enter 'Regular'
or 'Guest'."); return; // Exit the program
}
System.out.println("Total cost: Rs." + totalCost);
}
}

4(ii) pyramid program


public class PyramidPattern {
public static void main(String[] args) {
int rows = 5; // Adjust the number of rows as needed
for (int i = 1; i <= rows; i++) {
// Print spaces before the
stars for (int j = rows; j >
i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i) - 1;
k++) {
System.out.print("*");
}
System.out.println(); // Move to the next line
}
} }
5.create class restaurant , instance variable
package

SwiftFood; public

class Restaurant {

// Instance variables
private String
restaurantName; private
long restaurantContact;
private String
restaurantAddress; private
float rating;

// Constructor (optional, but recommended)


public Restaurant(String restaurantName, long restaurantContact,
String restaurantAddress, float rating) {
this.restaurantName =
restaurantName;
this.restaurantContact =
restaurantContact;
this.restaurantAddress =
restaurantAddress; this.rating =
rating;
}

// Method to display restaurant


details public void
displayRestaurantDetails() {
System.out.println("Restaurant Name: " +
restaurantName); System.out.println("Contact:
" + restaurantContact);
System.out.println("Address: " +
restaurantAddress);
System.out.println("Rating: " + rating);
} }
6.BOOK BIG QUST PROGRAM
import
java.util.Scanner;
public class Book
{
private String
author; private
String title; private
double price;
private String
publisher; private
int stock;

public Book(String author, String title, double price, String


publisher, int stock) { this.author = author;
this.title =
title; this.price
= price;
this.publisher =
publisher; this.stock =
stock;
}
public String getAuthor() {
return author;
}
public String
getTitle() { return
title;
}
public double
getPrice() {
return price;
}
public String
getPublisher() { return
publisher;
}

public int
getStock() {
return stock;
}
public boolean isAvailable(String title, String author) {
return this.title.equalsIgnoreCase(title) &&
this.author.equalsIgnoreCase(author);
}
public void displayDetails() {
System.out.println("Book
Details:");
System.out.println("Author: " +
author); System.out.println("Title:
" + title);
System.out.println("Price: " +
price);
System.out.println("Publisher: " +
publisher);
System.out.println("Stock: " +
stock);
}

public double calculateCost(int


copies) { if (copies <= stock)
{
return copies * price;
} else {
return 0.0; // Required copies not in stock
}
}
}
public class BookStore {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Sample book inventory (replace with
actual data) Book[] books = {
new Book("J.K. Rowling", "Harry Potter and the Philosopher's Stone",
25.0, "Bloomsbury", 10),
new Book("George Orwell", "1984", 18.5, "Penguin Books", 5),
new Book("Jane Austen", "Pride and Prejudice", 15.0, "Oxford
University Press",
7. a. customer class program
class Customer {
private String
customerId; private
String customerName;
private long
contactNumber; private
String address;

public Customer(String customerId, String customerName, long


contactNumber, String address) {
this.customerId = customerId;
this.customerName =
customerName;
this.contactNumber =
contactNumber; this.address =
address;
}

public void displayDetails() {


System.out.println("Customer ID: " +
customerId);
System.out.println("Customer Name: " +
customerName); System.out.println("Contact
Number: " + contactNumber);
System.out.println("Address: " + address);
System.out.println();
}

public static void main(String[] args) {


// Create two customer objects
Customer customer1 = new Customer("C001", "John Doe", 1234567890,
"123 Main
St");
Customer customer2 = new Customer("C002", "Jane Smith", 9876543210,
"456 Oak
Ave");
// Display customer details
customer1.displayDetails();
customer2.displayDetails();
}
}

7(ii)calculator
public class Calculator {

public double findAverage(double num1, double


num2, double num3) { return Math.round(((num1 +
num2 + num3) / 3) * 100.0) / 100.0;
}

public static void main(String[] args)


{ Calculator calculator = new
Calculator();
double average = calculator.findAverage(10.0, 20.0, 30.0);
System.out.println("Average: " + average);
}
}
8.EMPLOYE RECORED PROGRAM
public class

EmployeeRecord {

private double[] salaries;

public EmployeeRecord(double[]
salaries) { this.salaries = salaries;
}
public double
calculateAverageSalary() {
double sum = 0;
for (double salary :
salaries) { sum +=
salary;
}
return sum / salaries.length;
}
public int countEmployeesAboveAverage(double
averageSalary) { int count = 0;
for (double salary :
salaries) { if (salary >
averageSalary) {
count++;
}
}
return count;
}
public int countEmployeesBelowAverage(double
averageSalary) { int count = 0;
for (double salary :
salaries) { if (salary <
averageSalary) {
count++;
}
}
return count;
}
public static void main(String[] args) {
double[] salaries = {23500.0, 25080.0, 28760.0, 22340.0, 19890.0};
EmployeeRecord employeeRecord = new EmployeeRecord(salaries);

double averageSalary =
employeeRecord.calculateAverageSalary();
System.out.printf("The average salary of the employee is: %.2f\n",
averageSalary);

int aboveAverage =
employeeRecord.countEmployeesAboveAverage(averageSalary);
System.out.printf("The number of employees having salary greater than
the average is:
%d\n", aboveAverage);

int belowAverage =
employeeRecord.countEmployeesBelowAverage(averageSalary);
System.out.printf("The number of employees having salary lesser than the
average is:
%d\n", belowAverage);
}
}
9.STUDENTS DATA DISPLAY PROGRAM
class Student {
private int
rollno;

public int
getRollno() {
return rollno;
}
public void setRollno(int
rollno) { this.rollno =
rollno;
}
}
class Test extends
Student { private int
marks1; private int
marks2;
public int
getMarks1() {
return marks1;
}
public void setMarks1(int
marks1) { this.marks1 =
marks1;
}
public int
getMarks2() {
return marks2;
}
public void setMarks2(int
marks2) { this.marks2 =
marks2;
}
}

class Result extends


Test { private int
total;

public Result(int rollno, int marks1, int marks2)


{ setRollno(rollno);
setMarks1(marks1);
setMarks2(marks2);
this.total = marks1 + marks2;
}
public void display() {
System.out.println("Roll No: " + getRollno());
System.out.println("Marks 1: " + getMarks1());

System.out.println("Marks 2: " + getMarks2());


System.out.println("Total Marks: " + total);
}

public static void main(String[]


args) { Result result = new
Result(101, 85, 90);
result.display();
}
}
10.CLASS MEDIA PROGRAM
class Media {
protected String
title;
protected double price;

public Media(String title, double


price) { this.title = title;
this.price = price;
}

public void display() {


System.out.println("Title: " +
title);
System.out.println("Price: " +
price);
}
}

class Book extends


Media { private int
pages;

public Book(String title, double price, int


pages) { super(title, price);
this.pages = pages;
}

public void
display() {
super.display(
);
System.out.println("Pages: " + pages);
}
}

class Tape extends


Media { private float
time;

public Tape(String title, double price,


float time) { super(title, price);
this.time = time;
}

public void
display() {
super.display(
);
System.out.println("Time: " + time);
}

public class Main {


public static void main(String[] args) {
Book book = new Book("The Lord of the Rings",
29.99, 1170); Tape tape = new Tape("Symphony
No. 9", 14.99, 67.5f);

System.out.println("Book
Details:"); book.display();
System.out.println("\nTape
Details:"); tape.display();
}
}
13.a.geometrical sequence
public class GeometricSequence {

public static void main(String[] args) {


int n = 10; // Number of elements in the
sequence int currentTerm = 1;

System.out.print("Geometric
Sequence: "); for (int i = 0; i < n;
i++) {
System.out.print(currentTerm + " ");
currentTerm *= 2; // Double the current term for each iteration
}
}
}

13(ii)print the following numbers


public class NumberPattern {

public static void main(String[] args) {


int rows = 7; // Number of rows in the pattern

for (int i = 1; i <=


rows; i++) { for (int j
= 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println(); // Move to the next line
}
}
}
14.(i) CUSTOMER DETAILS NAME,PH-NO
class Customer {
private String
customerId; private
String customerName;
private long
contactNumber; private
String address;

public Customer(String customerId, String customerName, long


contactNumber, String address) {
this.customerId = customerId;
this.customerName =
customerName;
this.contactNumber =
contactNumber; this.address =
address;
}

public void displayDetails() {


System.out.println("Customer ID: " +
customerId);
System.out.println("Customer Name: " +
customerName); System.out.println("Contact
Number: " + contactNumber);
System.out.println("Address: " + address);
System.out.println();
}

public static void main(String[] args) {


// Create two customer objects
Customer customer1 = new Customer("C001", "John Doe", 1234567890,
"123 Main
St");
Customer customer2 = new Customer("C002", "Jane Smith", 9876543210,
"456 Oak
Ave");

// Display customer details


customer1.displayDetails();
customer2.displayDetails();
}
}

14.(ii) CLASS CALCULATOR


class Calculator {
public double findAverage(double num1, double
num2, double num3) { double sum = num1 + num2
+ num3;
double average = sum / 3.0;
return Math.round(average * 100.0) / 100.0; // Round to two decimal
places
}
}

class Tester {
public static void main(String[] args) {

Calculator calculator = new Calculator();


double avg = calculator.findAverage(10.5,
15.2, 20.3); System.out.println("Average: " +
avg);
}
}
15.MATRIX ADD,SUB PROGRAM
import java.util.Scanner;
public class MatrixOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get matrix dimensions from the
user System.out.print("Enter the
number of rows: "); int rows =
scanner.nextInt();
System.out.print("Enter the number of
columns: "); int cols =
scanner.nextInt();

// Create matrices
int[][] matrix1 = new
int[rows][cols]; int[][]
matrix2 = new
int[rows][cols];

// Get matrix elements from the user


System.out.println("Enter elements of matrix
1:"); getMatrixElements(scanner, matrix1);
System.out.println("Enter elements of matrix
2:"); getMatrixElements(scanner, matrix2);

// Perform matrix addition


int[][] sumMatrix = addMatrices(matrix1, matrix2);

// Perform matrix subtraction


int[][] differenceMatrix = subtractMatrices(matrix1, matrix2);

// Display results
System.out.println("\nMatrix 1:");
printMatrix(matrix1);
System.out.println("\nMatrix 2:");
printMatrix(matrix2);
System.out.println("\nSum of
Matrices:");
printMatrix(sumMatrix);
System.out.println("\nDifference of
Matrices:"); printMatrix(differenceMatrix);

scanner.close();

// Helper method to get matrix elements from the user


private static void getMatrixElements(Scanner scanner,
int[][] matrix) { for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j <
matrix[0].length; j++) {
matrix[i][j] =
scanner.nextInt();
}
}
}

// Helper method to add two matrices


private static int[][] addMatrices(int[][]
matrix1, int[][] matrix2) { int rows =
matrix1.length;
int cols =
matrix1[0].length;
int[][] sum = new
int[rows][cols];

for (int i = 0; i < rows;


i++) { for (int j = 0; j
< cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

return sum;
}

// Helper method to subtract two matrices


private static int[][] subtractMatrices(int[][]
matrix1, int[][] matrix2) { int rows =
matrix1.length;
int cols = matrix1[0].length;
int[][] difference = new int[rows][cols];

for (int i = 0; i < rows;


i++) { for (int j = 0; j
< cols; j++) {
difference[i][j] = matrix1[i][j] - matrix2[i][j];
}
}

return difference;
}

// Helper method to print a matrix


private static void
printMatrix(int[][] matrix) { for
(int[] row : matrix) {
for (int element : row) {
System.out.print(element +
" ");
}
System.out.println();
} } }
16. TRAIN PROGRAM
class Train {
private int
firstTierSeats; private
int secondTierSeats;
private int
thirdTierSeats;
public Train(int firstTierSeats, int secondTierSeats, int
thirdTierSeats) { this.firstTierSeats = firstTierSeats;
this.secondTierSeats =
secondTierSeats;
this.thirdTierSeats =
thirdTierSeats;
}
public void setFirstTierSeats(int
firstTierSeats) { this.firstTierSeats
= firstTierSeats;
}
public void setSecondTierSeats(int
secondTierSeats) { this.secondTierSeats =
secondTierSeats;
}
public void setThirdTierSeats(int thirdTierSeats)
{ this.thirdTierSeats = thirdTierSeats;
}
public void displayData() {
System.out.println("Number of First Tier Seats: " +
firstTierSeats); System.out.println("Number of Second Tier Seats:
" + secondTierSeats); System.out.println("Number of Third Tier
Seats: " + thirdTierSeats);
} }
class Reservation extends
Train { private int
seatsBookedFirst;
private int
seatsBookedSecond;
private int
seatsBookedThird;
public Reservation(int firstTierSeats, int secondTierSeats, int
thirdTierSeats) { super(firstTierSeats, secondTierSeats,
thirdTierSeats); this.seatsBookedFirst = 0;
this.seatsBookedSecond = 0;
this.seatsBookedThird = 0;
}
public boolean bookTicket(int tier, int numSeats) {
if (tier == 1 && seatsBookedFirst + numSeats <=
firstTierSeats) { seatsBookedFirst += numSeats;
return true;
} else if (tier == 2 && seatsBookedSecond + numSeats <=
secondTierSeats) { seatsBookedSecond += numSeats;
return true;
} else if (tier == 3 && seatsBookedThird + numSeats <=
thirdTierSeats) { seatsBookedThird += numSeats;
return true;
} else {
return false; // Insufficient seats
} }
public boolean cancelTicket(int tier, int
numSeats) { if (tier == 1 &&
seatsBookedFirst >= numSeats) {
seatsBookedFirst -=
numSeats; return true;
} else if (tier == 2 && seatsBookedSecond >= numSeats) {
seatsBookedSecond -= numSeats;
return true;
} else if (tier == 3 && seatsBookedThird >=
numSeats) { seatsBookedThird -=
numSeats;
return true;
} else {
return false; // Insufficient booked seats to cancel
} }
public void displayStatus() {
System.out.println("Seats Booked in First Tier: " + seatsBookedFirst);
System.out.println("Seats Booked in Second Tier: " +
seatsBookedSecond); System.out.println("Seats Booked in Third Tier: "
+ seatsBookedThird); System.out.println("Seats Available in First Tier: "
+ (firstTierSeats - seatsBookedFirst)); System.out.println("Seats
Available in Second Tier: " + (secondTierSeats -
seatsBookedSecond));
System.out.println("Seats Available in Third Tier: " +
(thirdTierSeats - seatsBookedThird));
}
public static void main(String[] args) {
Reservation reservation = new Reservation(50,
100, 150); reservation.displayData();

if (reservation.bookTicket(1, 20)) {
System.out.println("First Tier tickets booked successfully.");
} else {
System.out.println("First Tier ticket booking failed.");
}
if (reservation.bookTicket(2, 80)) {
System.out.println("Second Tier tickets booked successfully.");
} else {
System.out.println("Second Tier ticket booking failed.");
}
reservation.displayStatus();

if (reservation.cancelTicket(1, 10)) {
System.out.println("First Tier tickets canceled successfully.");
} else {
System.out.println("First Tier ticket cancellation failed.");
}
reservation.displayStatus();
}
}
17. CHOCOLATE COMPANY PROGRAM
public class
Chocolate {
private int
barCode; private
String name;
private double
weight; private
double cost;

// Default
constructor
public
Chocolate() {
this.barCode = 0;
this.name = "Default
Chocolate"; this.weight =
0.0;
this.cost = 0.0;
}

// Parameterized constructor
public Chocolate(int barCode, String name, double weight,
double cost) { this.barCode = barCode;
this.name = name;
this.weight =
weight; this.cost =
cost;
}
// Getters and Setters (if
needed) public int
getBarCode() {
return barCode;
}
public void setBarCode(int
barCode) { this.barCode =
barCode;
}
// ... other getters and setters for name, weight,
and cost public static void main(String[] args)
{
// Creating a Chocolate object using the
parameterized constructor Chocolate chocolate1 =
new Chocolate(101, "Cadbury", 12.0, 10.0);

// Creating a Chocolate object using the default


constructor Chocolate chocolate2 = new
Chocolate();

// Accessing and printing attributes of


chocolate1 System.out.println("Chocolate
1:");
System.out.println("Bar Code: " +
chocolate1.getBarCode());
System.out.println("Name: " +
chocolate1.name);
System.out.println("Weight: " +
chocolate1.weight); System.out.println("Cost:
" + chocolate1.cost);
// Accessing and printing attributes of
chocolate2 System.out.println("\nChocolate
2:");
System.out.println("Bar Code: " +
chocolate2.getBarCode());
System.out.println("Name: " +
chocolate2.name);
System.out.println("Weight: " +
chocolate2.weight); System.out.println("Cost:
" + chocolate2.cost);
} }

You might also like