diff --git a/QUESTION 1 b/QUESTION 1 new file mode 100644 index 0000000..383df1c --- /dev/null +++ b/QUESTION 1 @@ -0,0 +1,20 @@ +// This is the outer class named School +class School { + // This is the inner class named Student + class Student { + // This is a method in inner class Student + public void print() { + System.out.println("Hi! I am inner class STUDENT of outer class SCHOOL."); + + } + } +} + +public class Question211{ + public static void main(String[] args) { + // Create an object of inner class Student + School.Student s1= new School().new Student(); + // Access the 'print()' method of the inner class Student using the inner class object + s1.print(); + } +} diff --git a/QUESTION 2 b/QUESTION 2 new file mode 100644 index 0000000..1d73917 --- /dev/null +++ b/QUESTION 2 @@ -0,0 +1,28 @@ +// This is the class named School +class School { + // This is a method in class School + public void print() { + System.out.println("Hi! I class SCHOOL."); + } +} +// This is the class named Student +class Student { + // This is a method in class Student + public void print() { + System.out.println("Hi! I am class STUDENT"); + } +} + +public class Question212{ + public static void main(String[] args) { +// Create an object of class Student +Student stu = new Student(); +// Call 'print()' method of class Student +stu.print(); +// Create an object of class School +School sch = new School(); +// Call 'print()' method of class School +sch.print(); + + } +} diff --git a/QUESTION 3 b/QUESTION 3 new file mode 100644 index 0000000..844c8b8 --- /dev/null +++ b/QUESTION 3 @@ -0,0 +1,20 @@ +// This is the main class Question +public class Question213{ + public static void main(String[] args) { + // Object of the main class is created + Question213 q = new Question213(); + // Print method on object of Question class is called + q.studentMethod(); + } + + // 'print()' method is defined in class Question + void print(Question213 object){ + System.out.print("Well Done!"); + } + // Define a method named 'studentMethod()' in class Question + void studentMethod(){ + // Call the method named 'print()' in class Question + Question213 q = new Question213(); + q.print(q); + } +} diff --git a/QUESTION 4 b/QUESTION 4 new file mode 100644 index 0000000..a0d8984 --- /dev/null +++ b/QUESTION 4 @@ -0,0 +1,15 @@ +// This is the main class Question +public class Question214{ + public static void main(String[] args){ + Answer a = new Answer(10,"MCQ"); + } +} +class Answer{ + Answer(){ + System.out.println("You got nothing."); + } + Answer(int marks, String type){ + this(); + System.out.println("You got "+marks+" for an "+ type); + } +} diff --git a/QUESTION 5 b/QUESTION 5 new file mode 100644 index 0000000..38d12b7 --- /dev/null +++ b/QUESTION 5 @@ -0,0 +1,11 @@ +public class Question215{ + public static void main(String[] args) { + //Declare variable with name 'nptel', 'space' and 'java' and proper datatype. + String nptel,space,java; + //Initialize the variables with proper input + nptel="NPTEL"; + space=" "; + java="JAVA"; + System.out.print(nptel+space+java); + } +} diff --git a/README.md b/README.md index eff63fa..7cdb77d 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ -# SWAYAM-Programming_In_Java-NPTEL \ No newline at end of file +# SWAYAM-Programming_In_Java-NPTEL +# WEEK 2 +Java Week 2:Q1 To call the method print() in class Student following the concept of inner class. + +Java Week 2:Q2 To call the method print() of class Student first and then call print() method of class School. + +Java Week 2:Q3 To call print() method of class Question by creating a method named ‘studentMethod()’. + +Java Week 2:Q4 To call default constructor first and then any other constructor in the class Answer. + +Java Week 2:Q5 To debug the program which is intended to print 'NPTEL JAVA'. diff --git a/WEEK-1/QUESTION 1 b/WEEK-1/QUESTION 1 deleted file mode 100644 index 25865a9..0000000 --- a/WEEK-1/QUESTION 1 +++ /dev/null @@ -1,156 +0,0 @@ -# Java Week 1:Q1 To find the perimeter and area of a circle given a value of radius. -import java.util.Scanner; -public class Exercise1_1 { - public static void main(String[] args) { -Scanner s = new Scanner(System.in); - double radius= s.nextDouble(); - double perimeter; - double area; -//Calculate the perimeter - - perimeter = 2 * Math.PI * radius; -//Calculate the area - area = perimeter * radius; - - System.out.println(perimeter); - System.out.println(area); - } -} -# Java Week 1:Q2 To find the largest among three numbers x, y, and z. -import java.util.Scanner; -public class Exercise1_2 { - - public static void main(String[] args) { - Scanner s = new Scanner(System.in); - int x = s.nextInt(); - int y = s.nextInt(); - int z = s.nextInt(); - int result = 0; - //Use if...else ladder to find the largest among 3 numbers and store the largest number in a variable called result. - - if( x > y && x > z) - { - result = x; - } - else if( y > x && y > z) - { - result = y; - } - else if( z > y && z > x) - { - result = z; - } - else - { - result = x; - } - - System.out.print(result); - } - -} -# Java Week 1:Q3 Consider First n even numbers starting from zero(0) and calculate sum of all the numbers divisible by 3 from 0 to n. Print the sum. -import java.util.Scanner; -public class Exercise1_3 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n=sc.nextInt(); - int sum=0,t; - //Use for or while loop do the operation - for(int i=0;i<=n;i++) - { - t = 2*(i-1); - if(t%3 == 0) - { - sum += t; - } - } - System.out.println(sum); - } -} -# Java Week 1:Q4 To check whether the number is an Armstrong number or not. - -import java.util.Scanner; -public class Exercise1_4 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n=sc.nextInt(); - int result=0; - //Use while loop check the number is Armstrong or not. - //store the output(1 or 0) in result variable - int remainder,temp,count=0,i; - temp=n; - while(temp!=0) - { - temp/=10; - count++; - } - i=count; - temp=n; - while(count>0) - { - remainder=temp%10; - result += Math.pow(remainder, i); - temp/=10; - count--; - } - if(n==result) - result=1; - else - result=0; - - System.out.print(result); - - } -} -# Java Week 1:Q5 To help Ram , find the highest mark and average mark secured by him in "s" number of subjects. -import java.util.Scanner; -public class Exercise1_5{ - public static void main(String[] args) { - Scanner input = new Scanner(System.in); - double mark_avg; - int result; - int i; - int s; - //define size of array - s = input.nextInt(); - //The array is defined "arr" and inserted marks into it. - int[] arr = new int[s]; - - for(i=0;i arr[j]) - { - //swap elements - temp = arr[j-1]; - arr[j-1] = arr[j]; - arr[j] = temp; - } - } - } - result=arr[s-1]; - temp=0; - for(i=0;i