(Student Name) (Student Roll Number) (Institute Name)
(Student Name) (Student Roll Number) (Institute Name)
1
Task 1:
User-defined methods: -
The methods that are defined by the programmer are known as user-defined methods. These
methods are so because their sub-routines are defined by the user/programmer.
Method_name
Parameter_list
Return-type
And, the body (actual routine of the task)
{
//body of code
}
Java Code: -
2
int s = p + q + r;
//Returns the sum
return sum;
}
}
Output:-
Task 2:
PROGRAM EXPLANATION: -
//User.java
import java.util.Scanner;
3
public class User {
return name;
}
4
int len = ascii.length;
int evenSum = 0;
5
}
//UserMain.java
SAMPLE OUTPUT: -
6
Task 3:
PROGRAM EXPLANATION: -
//JAVA PROGRAM:
import java.util.Scanner;
import java.lang.*;
class Module
{
static String module_name;
static int module_marks;
static int module_credit;
static String module_name1;
static int module_marks1;
static int module_credit1;
static String module_name2;
static int module_marks2;
static int module_credit2;
static int total_credit = 0;
static double CGPA;
static void input()
{
Scanner sc = new Scanner(System.in);
//user input for n=1
System.out.print("Enter the first module name: ");
7
module_name = sc.next();
do
{
System.out.print("Enter the first module marks: ");
module_marks = sc.nextInt();
if(module_marks>100 || module_marks<0)
System.out.println("Invalid value. Marks should be between 0 and
100.");
}while(module_marks>100 || module_marks<0);
do
{
System.out.print("Enter the first module credit: ");
module_credit = sc.nextInt();
if(module_credit>15 || module_credit<10)
System.out.println("Invalid value. Credit should be between 10 and
15.");
}while(module_credit>15 || module_credit<10);
//user input for n=2
System.out.print("Enter the second module name: ");
module_name1 = sc.next();
do
{
System.out.print("Enter the second module marks: ");
module_marks1 = sc.nextInt();
if(module_marks1>100 || module_marks1<0)
System.out.println("Invalid value. Marks should be between 0 and
100.");
}while(module_marks1>100 || module_marks1<0);
do
{
System.out.print("Enter the second module credit: ");
module_credit1 = sc.nextInt();
if(module_credit1>15 || module_credit1<10)
System.out.println("Invalid value. Credit should be between 10 and
15.");
}while(module_credit1>15 || module_credit1<10);
//user input for n=3
System.out.print("Enter the third module name: ");
module_name2 = sc.next();
do
{
System.out.print("Enter the third module marks: ");
8
module_marks2 = sc.nextInt();
if(module_marks2>100 || module_marks2<0)
System.out.println("Invalid value. Marks should be between 0 and
100.");
}while(module_marks2>100 || module_marks2<0);
do
{
System.out.print("Enter the third module credit: ");
module_credit2 = sc.nextInt();
if(module_credit2>15 || module_credit2<10)
System.out.println("Invalid value. Credit should be between 10 and
15.");
}while(module_credit2>15 || module_credit2<10);
}
static void compute()
{
total_credit = module_credit + module_credit1 + module_credit2;
CGPA = ( (module_credit*module_marks) +
(module_credit1*module_marks1) +
(module_credit2*module_marks2) )/total_credit;
System.out.println("CGPA is "+ CGPA);
}
}
public class Test
{
public static void main(String[] args) {
Module ob1 = new Module();
ob1.input();
ob1.compute();
System.out.println("");
Module ob2 = new Module();
ob2.input();
ob2.compute();
}
}
9
The output of the program is attached.
OUTPUT
10
Task4:
Task 2 Reflection:
In task 2 it was been assigned to write a JAVA program in which I have written methods that
are working the way, which the methods were supposed to do, it this task I have been asked
to create functions which read the name of the student from keyboard, then the name should
be converted into an array for this I have to set all the name characters in the array. Then by
using ASCII values I have to convert the array name into an integer. Then I have to find the
sum of all those integers who are multiples of 6 and that are on the odd position of the array.
For the task I have done the work as:
The object of User class is created. Then, User class method to take name as
input from the user is used. The methods to convert string to char [], char [] to integer [],
display elements of char [], and int [] , and show Even multiple of six which are present at
odd position in int [] is also defined. The input from the user is taken in User class. The input
name entered by the user is validated if the length of name is at-least 12 chars long. If the
length of name is less than user enter name again. Then the object of user is created using the
valid name user entered. The char [] representation for the name is returned from user class
method nameToCharArray(). The displayChars (char []) shows content of chars []. Then
charsToAscii takes char [] as parameter and returns the int[] for that array. The
displayAsciis(int[]) shows integers value in nameAscii[]. In the end method
showEvenMultiplesOfSix display all even integers which are multiples of 6 present at odd
position in [].
11
Task 3 Reflection:
In task 3 I have been again assigned to create another class with creation of some unique
functionality. I have done whatever was asked in the task, my entire description of task 3 is
explained below as:
Create a class Module. Declare the instance variables for module name, module marks and
module credits for three subjects.
;static String module_name
;static int module_marks
;static int module_credit
Define a method, input (). Inside input () method, user input for all the variables for module
name, module marks and module credit. User input for module marks is taken inside a do-
while loop. Input validation is done using an if statement. Similarly, user input for module
credit is also taken inside a do-while loop. Input validation is done using an if statement.
Define a method, compute (). Inside compute () method, the values for variables, total credit
and CGPA are computed. The variable to hold total credit is initialized to the sum of all the
module credit values. Similarly, the value of variable, CGPA is computed as per the given
formula. The value of the CGPA variable is displayed to the user. Create a class Test. Inside
this class, main () method is defined. Inside main () method, two objects of the class Module
12
are created. Using these objects, the methods defined inside the class Module are called. The
java program is has been already described in task 3 with all the functionality.
Task5:
References:
1. (May, 2004)James Gosling private communication to Bill Bumgarner.
2. Professor devises easier calculator; www.physorg.com. June 2005.
3. Inc., HP. "HP Support document - HP Support Center". h20564.www2.hp.com.
Retrieved 2016-08-23.
4. "Texas Instruments releases new OS for TI-84, 2.55 MP". Tech Powered Math. 2011-
01-14. Retrieved 2018-05-12.
5. David A. Watt. Programming language concepts and paradigms, Prentice Hall; 1990.
Citation 13.
13