Assessment 1
Name: Nithish D
[Link].:18MIS0332
Slot: L7+L8
Course: Programming in java
Factorial with constraints:
public class Factwithcons
{
public static void main(String args[])
{
int a = 10;
int i;
long fact = 1;
if(a<0)
{
[Link]("please enter the positive number");
}
if(a>1000)
{
[Link]("the value should be less than 1000");
}
else
{
for(i=1;i<=a;i++)
{
fact = fact*i;
}
[Link]("value of the factorial is" + fact);
}
}
}
Hello world program:
class Myfirstjava
{
public static void main(String args[])
{
[Link]("hello world");
}
}
Factorial program:
public class Factorial
{
public static void main(String args[])
{
int num = 3;
long factorial = 1;
for(int i=1;i<=num;i++)
{
factorial = factorial*i;
}
[Link]("factorial of" + num + "is" + factorial);
}
};
Object Initialization:
public class Dog
{
String name;
String breed;
int age;
String color;
public Dog(String name, String breed, int age, String color)
{
[Link] = name;
[Link] = breed;
[Link] = age;
[Link] = color;
}
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
public int getAge()
{
return age;
}
public String getColor()
{
return color;
}
public String toString()
{
return("Hi my name is "+ [Link]()+
".\nMy breed,age and color are " +
[Link]()+"," + [Link]()+
","+ [Link]());
}
public static void main(String[] args)
{
Dog tuffy = new Dog("tuffy","papillon", 5, "white");
[Link]([Link]());
}
}
Nested class:
class Oclass
{
class Inner
{
public void show()
{
[Link]("in a nested class method");
}
}
public void out()
{
[Link]("o class");
}
}
class Main
{
public static void main(String[] args)
{
[Link] in = new Oclass().new Inner();
Oclass p = new Oclass();
[Link]();
[Link]();
}
}
Polymorphism:
public class Sum {
public int sum(int x, int y)
{
return (x + y);
}
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[])
{
Sum s = new Sum();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
Inheritance:
class Person
{
void walk()
{
[Link]("Can Run….");
}
}
public class Employee extends Person
{
void walk()
{
[Link]("Running Fast…");
}
public static void main(String arg[])
{
Person p = new Employee();
[Link]();
}
}
Encapsulation:
class Student
{
private String name;
public String getName()
{
return name;
}
public void setName(String name) {
[Link] = name;
}
}
public class Encapsulation
{
public static void main(String[] args)
{
Student s = new Student();
[Link]("Nithish");
[Link]([Link]());
}
}
One dimensional array:
class Student
{
public int roll_no;
public int marks;
public String name;
Student(int roll_no, String name,int marks)
{
this.roll_no = roll_no;
[Link] = name;
[Link] = marks;
}
}
public class GFG
{
public static void main (String[] args)
{
int avg=0,total = 0;
Student[] arr;
arr = new Student[5];
arr[0] = new Student(1,"aman",97);
arr[1] = new Student(2,"vaibhav",87);
arr[2] = new Student(3,"shikar",100);
arr[3] = new Student(4,"dharmesh",0);
arr[4] = new Student(5,"mohit",0);
for (int i = 0; i < [Link]; i++)
{
[Link]("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name +" "+arr[i].marks);
}
for (int i = 0; i < [Link]; i++)
total = total+arr[i].marks;
avg = total/3;
[Link]("total is:" + total);
[Link]("avg of 3 student:" +avg);
}
}
Multidimensional array:
class Matrixaddition
{
public static void main(String args[])
{
int arr1[][] = { {2,7,9},{3,6,1},{7,4,2} };
int arr2[][] = { {1,2,3},{1,2,3},{1,2,3} };
int arr3[][] = new int[3][3];
for (int i=0; i< 3 ; i++)
{
for (int j=0; j < 3 ; j++)
{
arr3[i][j] = arr1[i][j]+arr2[i][j];
[Link](arr3[i][j] + " ");}
[Link]("\n");
}
}
}
Calculator program:
import [Link];
class Calculator {
public static void main(String[] args) {
int num1=0, num2=0,num3=0;
Scanner scanner = new Scanner([Link]);
[Link]("Enter an operator (+, -, *, /,%): ");
char operator = [Link]().charAt(0);
if(operator == '%')
{
[Link]("Enter the number for factorial:");
num1 = [Link]();
}
else
{
[Link]("Enter first number:");
num2 = [Link]();
[Link]("Enter second number:");
num3 = [Link]();
}
[Link]();
int output=1;
switch(operator)
{
case '+':
output = (num2) + (num3);
break;
case '-':
output = num2 - num3;
break;
case '*':
output = num2 * num3;
break;
case '/':
output = num2 / num3;
break;
case '%':
int i;
for(i=1;i<=num1;i++)
{
output = output*i;
}
break;
default:
[Link]("You have entered wrong operator");
return;
}
if(operator == '%')
{
[Link]("the factorial of"+" "+num1+" "+output);
}
else
{
[Link](num2+" "+operator+" "+num3+": "+output);
}
}
}
Parametrized constructor:
class Parameter
{
int i,j,k;
Parameter()
{
[Link]("constructor without parameter");
}
Parameter(int i,int j)
{
this.i = i;
this.j = j;
[Link]("constructor with two parameter");
[Link](i+j);
}
Parameter(int i, int j,int k)
{
this.i = i;
this.j = j;
this.k = k;
[Link]("constructor with three parameter");
[Link](i+j+k);
}
public static void main(String[] args)
{
Parameter a = new Parameter();
Parameter b = new Parameter(2,3);
Parameter c = new Parameter(1,2,3);
}
}
Challenging exp:
import [Link];
class Zodiac
{
int month;
int day;
void output(int x,int y)
{
month = x;
day = y;
if((month==1)&&(day<=31&&day>=20) ||(month==2)&&(day<=18))
[Link]("Aquarius");
else if((month==2)&&(day<=29 && day>=19) || (month==3)&&(day<=20))
[Link]("Pisces");
else if((month==3) && (day<=31 && day>=21) || (month==4) && (day<=19))
[Link]("Aries");
else if((month==4) && (day<=30 && day>=20) || (month==5) && (day<=20))
[Link]("Taurus");
else if((month==5) && (day<=31 && day>=21) || (month==6) && (day<=20))
[Link]("Gemini");
else if((month==6) && (day<=30 && day>=21) || (month == 7) && (day<=22))
[Link]("Cancer");
else if((month==7) && (day<=31 && day>=23) || (month==8) && (day<=22))
[Link]("leo");
else if((month==8) && (day<=31 && day>=23) || (month==9) && (day<=22))
[Link]("virgo");
else if((month==9) && (day<=30 && day>=23) || (month==10) && (day<=22))
[Link]("libra");
else if((month==10) && (day<=31 && day>=23) || (month==11) && (day<=21))
[Link]("scorpio");
else if((month==11) && (day<=30 && day>=22) || (month==12) && (day<=21))
[Link]("sagittarius");
else if((month==12) && (day<=31 && day>=22) || (month == 1) && (day<=19))
[Link]("capricon");
}
public static void main(String args[])
{
Zodiac a = new Zodiac();
Scanner s = new Scanner([Link]);
int m = [Link]();
int n = [Link]();
[Link](m,n);
}
}