Java Assignment-3 (Programmes)
Java Assignment-3 (Programmes)
Java Code:
//Rectangle.java
public class Rectangle {
private double width;
private double height;
Here we are getting a step ahead in printing patterns as in generic we usually play with columnar
printing in the specific row keep around where elements in a row are either added up throughout
or getting reduced but as we move forward we do start playing with rows which hold for outer
loop in our program.
Illustrations:
Approach:
Implementation:
// Main class
public class GFG {
// Pyramid printing
for (int j = 0; j <= x; j++)
System.out.print((i + j) < 10
? (i + j) + " "
: (i + j) + " ");
Output
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
A positive integer with digits p, q, r, s…, is known as an Armstrong number of order n if the
following condition is fulfilled.
pqrs... = pn + qn + rn + sn +....
Example:
Examples:
Approach:
Firstly, we will traverse through all the numbers in the given range. Then, for every number, we
have to count the number of digits in it. If the number of digits in the current number is n then,
find the sum of (n-th) power of all the digits in the number stated. And if the sum is equal to the
current number i, then print the number.
Example:
// JAVA program to find Armstrong
// numbers between two integers
import java.io.*;
import java.math.*;
class gfg {
Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is
determined by multiplying the daily interest rate by the principal by the number of days that
elapse between payments.
Where,
Examples: –
Example 1:
Input : P = 10000
R = 5
T = 5
Output : 2500
Example 2:
Input : P = 3000
R = 7
T = 1
Output : 210
Example 1:
class GFG {
public static void main(String args[])
{
// We can change values here for
// different inputs
float P = 1, R = 1, T = 1;
Output
Simple interest = 0.01
Example 2:
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that
binds together code and the data it manipulates. Another way to think about encapsulation is, that
it is a protective shield that prevents the data from being accessed by the code outside this
shield.
• Technically in encapsulation, the variables or data of a class is hidden from any other class and
can be accessed only through any member function of its own class in which it is declared.
• As in encapsulation, the data in a class is hidden from other classes using the data hiding
concept which is achieved by making the members or methods of a class private, and the class is
exposed to the end-user or the world without providing any details behind implementation
using the abstraction concept, so it is also known as a combination of data-hiding and
abstraction.
• Encapsulation can be achieved by Declaring all the variables in the class as private and writing
public methods in the class to set and get the values of variables.
Output
Area: 32
// Java Encapsulation
// Person Class
class Person {
// Encapsulating the name and age
// only approachable and used using
// methods defined
private String name;
private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
// Driver Class
public class Main {
// main function
public static void main(String[] args)
{
// person object created
Person person = new Person();
person.setName("John");
person.setAge(30);
In Java, Method Overloading allows different methods to have the same name, but different
signatures where the signature can differ by the number of input parameters or type of input
parameters, or a mixture of both.
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Output
30
60
31.0
Different Ways of Method Overloading in Java
Method overloading can be achieved by changing the number of parameters while passing to
different methods.
// Class 1
// Helper class
class Product {
// Method 1
// Multiplying two integer values
public int multiply(int a, int b)
{
int prod = a * b;
return prod;
}
// Method 2
// Multiplying three integer values
public int multiply(int a, int b, int c)
{
int prod = a * b * c;
return prod;
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of above class inside main()
// method
Product ob = new Product();
// Calling method to Multiply 2 numbers
int prod1 = ob.multiply(1, 2);
// Class 1
// Helper class
class Product {
// Multiplying three integer values
public int Prod(int a, int b, int c)
{
int prod1 = a * b * c;
return prod1;
}
Method overloading can also be implemented by rearranging the parameters of two or more
overloaded methods. For example, if the parameters of method 1 are (String name, int roll_no) and
the other method is (int roll_no, String name) but both have the same name, then these 2 methods
are considered to be overloaded with different sequences of parameters.
// Class 1
// Helper class
class Student {
// Method 1
public void StudentId(String name, int roll_no)
{
System.out.println("Name :" + name + " "
+ "Roll-No :" + roll_no);
}
// Method 2
public void StudentId(int roll_no, String name)
{
// Again printing name and id of person
System.out.println("Roll-No :" + roll_no + " "
+ "Name :" + name);
}
}
// Class 2
// Main class
class GFG {
// Main function
public static void main(String[] args)
{
// Creating object of above class
Student obj = new Student();
• Type Conversion but to a higher type(in terms of range) in the same family.
• Type conversion to the next higher family(suppose if there is no long data type available for an
int data type, then it will search for the float data type).
Let’s take an example to clarify the concept:
// Demo Class
class Demo {
class UseDemo {
byte a = 25;
// it will go to
// byte argument
obj.show(a);
// String
obj.show("hello");
// Int
obj.show(250);
// Since char is
// range is int.
obj.show('A');
// String
obj.show("A");
// will be an error.
obj.show(7.5);
Output