[go: up one dir, main page]

0% found this document useful (0 votes)
7 views13 pages

B1 078 Mane Java Assignment 3

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)
7 views13 pages

B1 078 Mane Java Assignment 3

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/ 13

Name Prathamesh Mane

UID no. & Branch 2022200078 (B1)

Experiment No. 3

AIM: Learn and implement the concepts of Constructor-Method Overloading

Program 1

PROBLEM Given a class Line with slope, y-intercept, x1, y1, x2, y2 as attributes, write
STATEMENT: 3 constructors for equations for the line given Slope-y-intercept, Slope
Point and two Point forms

 Slope-y-intercept:
y = mx + c
 Slope point form:
y - y1 = m(x - x1)
 Two Point form:
(y - y1) / (y1 - y2) = (x - x1) / (x1 - x2)

Each constructor should display the appropriate Line equation.

PROGRAM: import java.util.Scanner;

class test {
public static void main(String[] args) {
double slope , y_intercept , x1 , y1 , x2 , y2 ;
int choice;

Scanner scan= new Scanner(System.in);

System.out.println("Enter the choice :\n"


+"1.Line in y intercept form\n"
+"2.Line in slope point form\n"
+"3.Line in two point form\n");
choice=scan.nextInt();

if(choice==1) {
System.out.println("Enter the slope of the line : ");
slope=scan.nextDouble();
System.out.println("Enter the Y intercept of the line : ");
y_intercept=scan.nextDouble();
Line pratham1 = new Line(slope,y_intercept);
}

else if(choice==2) {
System.out.println("Enter the slope of the line : ");
slope=scan.nextDouble();
System.out.println("Enter the point y1 : ");
y1=scan.nextDouble();
System.out.println("Enter the point x1 : ");
x1=scan.nextDouble();
Line pratham2 = new Line(slope,y1,x1);
}

else if(choice==3) {
System.out.println("Enter the point y1 : ");
y1=scan.nextDouble();
System.out.println("Enter the point x1 : ");
x1=scan.nextDouble();
System.out.println("Enter the point y2 : ");
y2=scan.nextDouble();
System.out.println("Enter the point x2 : ");
x2=scan.nextDouble();
Line pratham3 = new Line(y1 ,x1 ,y2 ,x2);
}

else {
System.out.println("You have entered the wrong option ");
}
}
}

class Line {
private double slope , y_intercept , x1 , y1 , x2 , y2 ;

Line() {};

Line(double slope , double intercept) {


this.slope=slope;
System.out.println("The slope with y intercept form (y = mx + c) is : \n
y = "+slope+"x + "+intercept+"\n");
}

Line(double slope,double y1,double x1) {


this.slope=slope;
this.y1=y1;
this.x1=x1;
System.out.println("The slope with point form (y - y1 = m(x - x1))
is :\n(y - "+y1+") = "+slope+" (x - "+x1+")\n");
}

Line(double y1,double y2,double x1, double x2) {


this.y1=y1;
this.x1=x1;
this.y2=y2;
this.x2=x2;

double m = (y2 - y1) / (x2 - x1);


System.out.println("The slope of the line is : " +m);
System.out.println("The slope with point form ((y - y1) / (y1 - y2) =
m(x - x1) / (x1 - x2)) is :\n(y - "+y1+") / ("+y1+" - "+y2+")= "+m+"
("+x1+" - "+x2+")\n");
}

}
RESULT:

CASE 1 : Slope in y intercept

CASE 2 : Slope in Point form


CASE 3 : Two point form

CASE 4 : Invalid Choice


Program 2

PROBLEM Create a Test class with a data double base, int power, int logBase, int
STATEMENT: argument. Create a default, no-argument constructor which sets the default
value of all variables to 2.

There are 2 overloaded functions:


 double calculate (double base, int power)This function returns the
value when base is raised to power For example: calculate (3.0, 2)
returns the value of 3.0 raised to 2 i.e., 9.0
 double calculate (int logBase, int argument)This function returns the
value of the log of argument to the base logBase

For example: calculate (3, 9) returns log of 9 to the base 3 i.e., 2.0
Create a main method in a separate class to call the above functions with the
following inputs:
 calculate (2, 4)
 calculate (2.0, 4)

Create a display() method which displays the output based on the type of
Test object created

PROGRAM: class pratham {


public static void main(String[] args) {
Test test = new Test();

double result1 = test.calculate(2.0,4); // 2^4


double result2 = test.calculate(2,9); // log base 2 of 9

System.out.print("Result of 2.0 raised to 4: ");


test.display(result1);

System.out.print("Log base 2 of 9: ");


test.display(2, 9);
}
}

class Test {
private double base;
private int power;
private int logBase;
private int argument;

// Default constructor
Test() {
this.base = 2.0;
this.power = 2;
this.logBase = 2;
this.argument = 2;
}

double calculate(double base, int power) { //method 1


return Math.pow(base, power);
}

double calculate(int logBase, int argument) { //method 2


return Math.log(argument) / Math.log(logBase);
}

void display(double result) {


System.out.println("Result: " + result);
}

void display(int logBase, int argument) {


double result = calculate(logBase, argument);
System.out.println("Logarithm of " + argument + " to the base " +
logBase + " is: " + result);
}
}

RESULT:
Program 3

PROBLEM Write a java code that allows users to select a pen based on their
STATEMENT: preferences by choosing from several options: a default pen, specifying
only the ink color, specifying the ink color, brand, and price, or creating an
exact replica of a predefined pen. Show case the use of method
overloading .

PROGRAM: import java.util.Scanner;

class Pratham {
public static void main(String[] args) {
String colour, brandName;
double price;
int choice;
Scanner scan = new Scanner(System.in);

System.out.println("Enter the choice:\n"


+ "1. Any pen will work\n"
+ "2. Only ink colour specification\n"
+ "3. Ink colour, Brand and price specification\n"
+ "4. You want an exact replica of the pen");

System.out.println("=====================================");
choice = scan.nextInt();

if (choice == 1) {

System.out.println("=====================================");
Pen p1 = new Pen();
p1.display();
System.out.println("\nThanks for buying, visit again:");

System.out.println("=====================================");
}

else if (choice == 2) {

System.out.println("=====================================");
System.out.println("Enter the colour:");
colour = scan.next();
Pen p2 = new Pen(colour);
p2.display();
System.out.println("\nThanks for buying, visit again:");

System.out.println("=====================================");
}

else if (choice == 3) {

System.out.println("=====================================");
System.out.println("Enter the colour:");
colour = scan.next();

System.out.println("Enter the Brand Name:");


brandName = scan.next();

System.out.println("Enter the Price:");


price = scan.nextDouble();

Pen p3 = new Pen(colour, brandName, price);


p3.display();
System.out.println("\nThanks for buying, visit again:");

System.out.println("=====================================");
}

else if (choice == 4) {

System.out.println("=====================================");
Pen p3 = new Pen("Black", "Parker", 20.0);
Pen p4 = new Pen(p3);
p4.display();
System.out.println("\nThanks for buying, visit again:");

System.out.println("=====================================");
}

else{
System.out.println("=====================================");
System.out.println("Invalid choice");
}

}
}

class Pen {
String inkColor, brand;
double price;

// Default constructor
Pen() {
inkColor = "Red";
brand = "Flair gello";
price = 7.8; // Example price
}

// Constructor with colour


Pen(String inkColor) {
this.inkColor = inkColor;
this.brand = "Giffy gel";
this.price = 10.0;
}

// Constructor with colour, brand, and price


Pen(String inkColor, String brand, double price) {
this.inkColor = inkColor;
this.brand = brand;
this.price = price;
}

// Copy constructor
Pen(Pen p) {
this.inkColor = p.inkColor;
this.brand = p.brand;
this.price = p.price;
}
// Display method
void display() {
System.out.println("Color = " + inkColor + "\nBrand = " + brand +
"\nPrice = " + price);
}
}

RESULT:

CASE 1 : Any default pen which is predefined (default constructor )


CASE 2 : ink specification for the pen (constructor with 1 argument of ink)

CASE 4 : Exact replica of the pen (constructor with argument as an object)


CASE 3 : Ink , brand and price specification (constructor with 3 arguments)

CONCLUSION: Method and constructor overloading are powerful features in programming


that allow us to use the same method or constructor name with different
parameters, making our code more flexible and easier to manage. In the
equation of a line code, constructor overloading helps us create line objects
using different inputs, like points or slope-intercept form. In the pen
management code, it lets us handle various pen details, such as color and
brand, based on what the user specifies. In the maths calculation code,
method overloading allows us to perform different operations like addition
or area calculations with the same method name but different inputs. This
approach keeps our code organized, versatile, and efficient.

You might also like