0% found this document useful (0 votes)
4 views11 pages

Aniruddhiyer A3 Psoopl Exp2A

The document outlines three programming experiments demonstrating the use of constructors in Java. Experiment 1 involves creating a Circle class with methods for area and circumference, Experiment 2 involves a User and Movie class for a movie platform, and Experiment 3 involves a User and Post class for a social media application. The conclusion reflects on the learning experience regarding constructors and their real-world applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Aniruddhiyer A3 Psoopl Exp2A

The document outlines three programming experiments demonstrating the use of constructors in Java. Experiment 1 involves creating a Circle class with methods for area and circumference, Experiment 2 involves a User and Movie class for a movie platform, and Experiment 3 involves a User and Post class for a social media application. The conclusion reflects on the learning experience regarding constructors and their real-world applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Name: Aniruddh Iyer

UID: 2023300078

Experiment No. 2A

AIM: To Write a program to demonstrate constructors

Program 1

PROBLEM Create a Circle class and include appropriate constructors. Also include the
STATEMENT : methods area and perimeter.

PROGRAM: import java.util.Scanner;

class Circle
{
double x,y;
double r;
Circle(double x,double y,double r)
{
this.x=x;this.y=y;this.r=r;
}
Circle(double r)
{
this.r=r;x=0;y=0;
}
Circle(Circle c)
{
x=c.x;y=c.y;r=c.r;
}
Circle()
{
x=3.0;y=4.0;r=5.0;
}
double area()
{
return 3.14*r*r;
}
double circum()
{
return 6.28*r;
}
}
class TestCircle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Circle c1=new Circle(3.0,4.0,5.0);
Circle c2=new Circle(5.0);
Circle c3=new Circle(c1);
Circle c4=new Circle();
double area=c1.area();
double circum=c1.circum();
System.out.println("Area:"+area);
System.out.println("Circumference:"+circum);
double area2=c2.area();
double circum2=c2.circum();
System.out.println("Area:"+area);
System.out.println("Circumference:"+circum);
double area3=c3.area();
double circum3=c3.circum();
System.out.println("Area:"+area);
System.out.println("Circumference:"+circum);
double area4=c4.area();
double circum4=c4.circum();
System.out.println("Area:"+area);
System.out.println("Circumference:"+circum);
}
}

RESULT:
Program 2

PROBLEM The "User" class represents a user on a „buy and stream‟ movie platform
STATEMENT : with attributes : name, age, account balance. The “Movie” class represents
a movie on the platform with attributes: Movie Title, AgeRestriction, Cost of
the movie. The User class should have a method to check whether he can
watch a movie based on his age and also account balance. The User class
should also have a method to WatchMovie where he has to pay the cost
for the Movie to watch it. The Movie class should have methods to get Cost
and Age restriction. The main method should create objects of the "User"
and "Movie" classes and demonstrate the use of their methods. Input
should be taken from the user.

PROGRAM: import java.util.Scanner;

class User
{
int age;
double AccBal;
String name = new String();
User (String name, double AccBal, int age)
{
this.name=name;
this.AccBal=AccBal;
this.age=age;
}
double checkBal(double price) {
if(balance<price) {
return 1;
}
return 0;
}
void NewAccBal(double price)
{
AccBal=AccBal-price;
}
}

class Movie
{
String MovieTitle = new String();
int AgeRestriction;
double cost;
Movie (String MovieTitle, int AgeRestriction, int cost)
{
this.MovieTitle=MovieTitle;
this.AgeRestriction=AgeRestriction;
this.cost=cost;
}
}

class MovieChoice
{
public static void main(String arr[])
{
Scanner sc= new Scanner(System.in);
Movie m1 = new Movie("Chotta Bheem" , 7 , 150);
Movie m2 = new Movie("Tiger ", 15, 475);
Movie m3 = new Movie("Deadpool", 18, 950);

System.out.println("Please enter your name ");


String name = new String();
name = sc.nextLine();
System.out.println("Please enter your Age ");
int age=sc.nextInt();
System.out.println("Please enter your Account Balance ");
double AccBal=sc.nextDouble();

User u1 = new User(name, AccBal, age);


System.out.println("Here are the movies available: ");
System.out.println("1:"+m1.MovieTitle);
System.out.println("2:"+m2.MovieTitle);
System.out.println("3:"+m3.MovieTitle);
System.out.print("Enter the number corresponding to the movie you
want to watch: ");
int i = sc.nextInt();
switch (i) {
case 1:
if(u1.AccBal<m1.cost && u1.age>=m1.AgeRestriction) {
System.out.println("Not enough Account Balance! ");
}
else if(u1.AccBal>=m1.cost && u1.age<m1.AgeRestriction) {
System.out.println("Not old enough to watch this!");
}
else if(u1.AccBal<m1.cost && u1.age<m1.AgeRestriction) {
System.out.println("You are ineligible to watch this film! ");
}
else {
System.out.println("Enjoy your movie!");
u1.NewAccBal(m1.cost);
System.out.println("Cost for one ticket is "+m1.cost);
System.out.println("Updated Balance is "+u1.AccBal);
}
break;
case 2:
if(u1.AccBal<m2.cost && u1.age>=m2.AgeRestriction) {
System.out.println("Not enough Account Balance! ");
}
else if(u1.AccBal>=m2.cost && u1.age<m2.AgeRestriction) {
System.out.println("Not old enough to watch this!");
}
else if(u1.AccBal<m2.cost && u1.age<m2.AgeRestriction) {
System.out.println("You are ineligible to watch this film! ");
}
else {
System.out.println("Enjoy your movie!");
u1.NewAccBal(m2.cost);
System.out.println("Cost for one ticket is "+m2.cost);
System.out.println("Updated Balance is "+u1.AccBal);
}
break;
case 3:
if(u1.AccBal<m3.cost && u1.age>=m3.AgeRestriction) {
System.out.println("Not enough Account Balance!");
}
else if(u1.AccBal>=m3.cost && u1.age<m3.AgeRestriction) {
System.out.println("Not old enough to watch this!");
}
else if(u1.AccBal<m3.cost && u1.age<m3.AgeRestriction) {
System.out.println("You are ineligible to watch this film! ");
}
else {
System.out.println("Enjoy your movie!");
u1.NewAccBal(m3.cost);
System.out.println("Cost for one ticket is "+m3.cost);
System.out.println("Updated Balance is "+u1.AccBal);
}
break;
default:
break;
}
}
}

RESULT:
Program 3

PROBLEM The "User" class represents a social media user with attributes: username,
STATEMENT: password. The class should have a method to get username. The "Post"
class represents a social media post with attributes such as postID,
LikesCount, and CommentsCount. It should have methods to display no. of
likes and comments for a post. The User class also has methods to
like/comment on posts. The main method should create objects of the
"User" and "Post" classes and demonstrate the use of their methods

PROGRAM: import java.util.Scanner;


class User
{
String username;
String password;

User() //default constructor


{}

User(String username,String password)


{
this.username=username;
this.password=password;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your username");
username=sc.nextLine();
System.out.println("Enter your password");
password=sc.nextLine();
}

void likepost(Post post)


{
System.out.println("User: " + username + " has liked your Post with post
ID " + post.postID);
}

void commentpost(Post post,String comment)


{
System.out.println("User: " + username + "has commented on your Post
with post ID " + post.postID + " with comment : " +comment);
}
}

class Post
{
int postID;
int likesCount;
int commentsCount;

Post() //default constructor


{}

Post(int postID, int likesCount,int commentsCount)


{
this.postID=postID;
this.likesCount=likesCount;
this.commentsCount=commentsCount;
}
void numberoflikes(int numLikes)
{
likesCount = numLikes;
}

void numberofcomments(int numComments)


{
commentsCount = numComments;
}
}

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

User u1=new User();


Post p1=new Post(283701923,198,57);
u1.input();
u1.likepost(p1);
u1.commentpost(p1,"What a Beautiful picture!!");
System.out.println("Number of likes on your first post are
"+p1.likesCount);
System.out.println("Number of comments on your first post are
"+p1.commentsCount);

User u2=new User();


Post p2=new Post(284739091,230,92);
u2.input();
u2.likepost(p2);
u2.commentpost(p2,"Lovely Trip!!");
System.out.println("Number of likes on your second post are
"+p2.likesCount);
System.out.println("Number of comments on your second post are
"+p2.commentsCount);
}
}

RESULT:

CONCLUSION: From the above experiment I learnt about different types of constructors,
their application and its implementation in programs which can be used to
solve real world problems.

You might also like