0% found this document useful (0 votes)
32 views

(Student Name) (Student Roll Number) (Institute Name)

Here are the key things I learned from completing Task 3: 1. How to take user input for multiple variables using a loop or multiple input statements. In this program, user input is taken for the name, marks and credits of 3 modules. Appropriate validation is also done to ensure the input is in the expected range. 2. How to define class variables and initialize them. The module details like name, marks, credits are defined as static class variables of the Module class. 3. How to calculate the total credits and CGPA from the module details. A compute method calculates the total credits by adding individual module credits. It then calculates the CGPA by dividing the weighted total marks by total credits.

Uploaded by

Hahsadasd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

(Student Name) (Student Roll Number) (Institute Name)

Here are the key things I learned from completing Task 3: 1. How to take user input for multiple variables using a loop or multiple input statements. In this program, user input is taken for the name, marks and credits of 3 modules. Appropriate validation is also done to ensure the input is in the expected range. 2. How to define class variables and initialize them. The module details like name, marks, credits are defined as static class variables of the Module class. 3. How to calculate the total credits and CGPA from the module details. A compute method calculates the total credits by adding individual module credits. It then calculates the CGPA by dividing the weighted total marks by total credits.

Uploaded by

Hahsadasd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

[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.    

To define a method the user should provide: -

 Method_name
 Parameter_list
 Return-type
 And, the body (actual routine of the task)

Syntax: - public static <return-type> <name> (<parameter definition>)

            {

                    //body of code

      }

Java Code: -

//Importing the package


import java.util.Scanner;
//Defining the main class
class Main
{
    //Defining the main function
    public static void main(String[] args)
  {
        //Creating the object of Scanner class
        Scanner f = new Scanner(System.in);
        System.out.print("Please enter Three numbers: ");
        //Declaring the variables 
        //And reading values entered by the user
        int x = f.nextInt();
        int y = f.nextInt();
        int z = f.nextInt();
        //Calling the methods and passing the parameters
        int sum = Add(x, y, z); 
        //displaying the sum 
        System.out.println("Sum is: " + sum);
  }
    //Defining the function 
    public static int Add(int p, int q, int r)
  {
        //Calculating the sum of three numbers 

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 {

 //take user name as input


 public String getUserName() {
  Scanner sc = new Scanner(System.in);
  String name = "";
  // loop executes till minimum length is less than 12 chars
  while (true) {
   System.out.println("Enter Your Name(min 12 chars long): ");
   name = sc.nextLine();

   // name length is less than 15


   if (name.length() < 15) {
    System.out.println("The name should be atleast 12 characters long");
   } else {
    break;
   }
  } // loop ends

  return name;

 }

 // convert string to char[]


 public char[] nameToCharArray(String name) {
  return name.toCharArray();
 }

 // convert chars to integers


 public int[] charsToAscii(char[] nameChars) {
  int namelength = nameChars.length;
  int ascii[] = new int[namelength];

  for (int i = 0; i < namelength; i++) {


   int x = (int) nameChars[i];
   // store ascii value in ascii[]
   ascii[i] = x;
 }
  return ascii;
 }

 // all even integers which are multiples of 6,


 // which are present at odd position
 public void showEvenMultiplesOfSix(int[] ascii) {

4
  int len = ascii.length;
  int evenSum = 0;

  // traverse the int[] in loop


  for (int i = 0; i < len; i++) {
   // if i is the odd position
   if (i % 2 != 0) {
    int temp = ascii[i];
    // check if temp is multiple of 6
    if (temp % 2 == 0 && temp % 3 == 0) {
     evenSum += temp;
  }
   }
 }
  System.out.println("\nSum of all Even Integers,multiples of 6, present at odd
position is  " + evenSum);
 }

 // display characters[]


 public void displayChars(char[] nameChars) {
  System.out.print("\nElements of Chars[]: \n\t[");
  int namelength = nameChars.length;

  for (int i = 0; i < namelength; i++) {


   System.out.print(nameChars[i]);
   if (i != namelength - 1) {
    System.out.print(",");
   }
 }
  System.out.println("]");
 }

 // display characters[]


 public void displayAsciis(int[] nameAscii) {
  System.out.print("\nElements of Ascii[]: \n\t");
  int namelength = nameAscii.length;

  for (int i = 0; i < namelength; i++) {


   System.out.print(nameAscii[i]);
   if (i != namelength - 1) {
    System.out.print(",");
   }
 }
  System.out.println("]");

5
 }

 
//UserMain.java

public class UserMain {

 public static void main(String[] args) {


    
  //create User object
  User user = new User();
  String name = user.getUserName();
  
  //string to char[]
  char nameChars[] = user.nameToCharArray(name);
  user.displayChars(nameChars);
  
  //chars[] to int[]
  int nameAscii[] =user.charsToAscii(nameChars);
  user.displayAsciis(nameAscii);
  
  //sum of all even ints which are multiples of 6
  //present at odd position in []
  user.showEvenMultiplesOfSix(nameAscii);
  
 }
}

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    

;static String module_name1    


;static int module_marks1    
     ;static int module_credit1    

;static String module_name2    


;static int module_marks2    
;static int module_credit2    

Declare variables to hold the total credit and CGPA value.

static int total_credit = 0;


static double CGPA;

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

You might also like