Different Java class examples for practice
Classes like:
Student , Rectangle , Car , Dog , Book , BankAccount , Product , Address, Ticket , Team.
1.
class Student {
String name;
int age;
voiddisplayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class StudentExample {
public static void main(String[] args) {
Student s = new Student();
s.name = "John";
s.age = 20;
s.displayInfo();
}
}
2.
class Rectangle {
double width;
double height;
doublecalculateArea() {
return width * height;
}
}
public class RectangleExample {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.width = 4.0;
r.height = 6.0;
System.out.println("Area of rectangle: " + r.calculateArea());
}
}
3.
class Car {
String make;
String model;
voiddisplayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
}
}
public class CarExample {
public static void main(String[] args) {
Car car = new Car();
car.make = "Toyota";
car.model = "Corolla";
car.displayInfo();
}
}
4.
class Dog {
String name;
String breed;
voiddisplayInfo() {
System.out.println("Name: " + name);
System.out.println("Breed: " + breed);
}
}
public class DogExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Buddy";
dog.breed = "Golden Retriever";
dog.displayInfo();
}
}
5.
class Book {
String title;
String author;
voiddisplayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
}
}
public class BookExample {
public static void main(String[] args) {
Book book = new Book();
book.title = "The Great Gatsby";
book.author = "F. Scott Fitzgerald";
book.displayInfo();
}
}
6.
class BankAccount {
String accountNumber;
double balance;
voiddisplayBalance() {
System.out.println("Balance: $" + balance);
}
}
public class BankAccountExample {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.accountNumber = "123456789";
account.balance = 1000.0;
account.displayBalance();
}
}
7.
class Product {
String name;
double price;
voiddisplayInfo() {
System.out.println("Product: " + name);
System.out.println("Price: $" + price);
}
}
public class ProductExample {
public static void main(String[] args) {
Product product = new Product();
product.name = "Laptop";
product.price = 800.0;
product.displayInfo();
}
}
8.
class Address {
String street;
String city;
voiddisplayInfo() {
System.out.println("Street: " + street);
System.out.println("City: " + city);
}
}
public class AddressExample {
public static void main(String[] args) {
Address address = new Address();
address.street = "123 Main St";
address.city = "New York";
address.displayInfo();
}
}
9.
class Ticket {
intticketNumber;
String eventName;
String ticketType;
doubleticketPrice;
public Ticket(int number, String event, String type, double price) {
ticketNumber = number;
eventName = event;
ticketType = type;
ticketPrice = price;
}
public void displayTicketInfo() {
System.out.println("Ticket Number: " + ticketNumber);
System.out.println("Event Name: " + eventName);
System.out.println("Ticket Type: " + ticketType);
System.out.println("Ticket Price: $" + ticketPrice);
}
}
public class TicketApplication {
public static void main(String[] args) {
Ticket regularTicket = new Ticket(101, "Concert", "Regular", 50.0);
Ticket vipTicket = new Ticket(102, "Sports Game", "VIP", 100.0);
System.out.println("Regular Ticket Info:");
regularTicket.displayTicketInfo();
System.out.println();
System.out.println("VIP Ticket Info:");
vipTicket.displayTicketInfo();
}
}
10.
class Team {
String teamName;
Player[] players;
Team(String teamName, Player[] players) {
this.teamName = teamName;
this.players = players;
}
voiddisplayTeamInfo() {
System.out.println("Team Name: " + teamName);
System.out.println("Players:");
for (Player player : players) {
player.displayInfo();
}
}
}
Sir,please provide MCQ's
ReplyDelete