Docsity

Data communication quiz answers, Quizzes of Data Communication Systems and Computer Networks

Toggle sidebar
bg1
Question 1:
a) Write a java application program to demonstrate the use of the following three methods:
• A method to find the greatest common divisor of given two numbers
• A method to find the sum of the series: sum = 0.11 + 0.22 + 0.33 + … + 2.2
• A recursive method for computing a Fibonacci number fib(n), given index n.
o fib(0) = 0; base condition
o fib(1) = 1; base condition
o fib(n) = fib(n-2) + fib(n-1); n >= 2.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Data communication quiz answers and more Quizzes Data Communication Systems and Computer Networks in PDF only on Docsity!

Question 1 :

a) Write a java application program to demonstrate the use of the following three methods:

  • A method to find the greatest common divisor of given two numbers
  • A method to find the sum of the series: sum = 0.11 + 0.22 + 0.33 + … + 2.
  • A recursive method for computing a Fibonacci number fib(n), given index n.

o fib(0) = 0; base condition

o fib(1) = 1; base condition

o fib(n) = fib(n-2) + fib(n-1); n >= 2.

public class Gcd_Series_Fib { public static int Gcd(int a, int b){ int gcd = 1 ; for(int i = 1 ; i <= a && i <= b;i++) { if(a%i== 0 &&b%i== 0 ) gcd = i; } return gcd; } public static double Series(double start, double end, double inc) { double sum = 0 ; for(double i = start; i <= end; i += inc){ sum+= i; } return sum; } public static int Fib(int n) { if(n == 0 || n == 1 ) return n; else return (Fib(n- 2 )+ Fib(n- 1 )); } public static void main(String[] args) { System.out.println("GCD(8,4): " + Gcd( 8 , 4 )); System.out.println("Sum of [0.11 + 0.22 + 0.33 +...+ 2.2]= " + Series(0.11, 2.2, 0.11)); System.out.println("Fib(0): " + Fib( 0 )); System.out.println("Fib(3): " + Fib( 3 )); } }

Another solution

❖ This code to input all data (Key answer, No. of questions, No. of students)

public class grades { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter number of answers: "); int k = input.nextInt(); char [] answers = new char[k]; for(int i = 0 ; i < k; i++) { answers[i] = input.next().charAt( 0 ); } System.out.print("Enter number of students: "); int n = input.nextInt(); int m = k; char [][] list = new char[n][m]; System.out.println("Fill in the "+ n +" student's answers to "+ m + " questions") ; for (int i = 0 ;i<list.length;i++) { int count = 0 ; for (int j= 0 ;j<list[i].length;j++) { list[i][j] = input.next().charAt( 0 ); if(list[i][j]==(answers[j])) count++; } System.out.println("Student's " + (i+ 1 ) + " correct answers are " + count); } } }

Question 2:

a) Explain the following object oriented concepts: object, class, inheritance,

and polymorphism.

Object: represents an entity in the real world that can be distinctly identified. For

example, a student, a desk, a circle, a button, and even a loan can all be viewed

as objects.

• An object has a unique identity, state, and behaviors.

o The state of an object consists of a set of data fields (also known as

properties) with their current values.

o The behavior of an object is defined by a set of methods.

Classes: Classes are constructs that define objects of the same type.

Inheritance: is the ability in object-oriented programming to derive new classes

from existing classes.

Polymorphism: An object of a subclass can be used by any code designed to

work with an object of its superclass.

public class Account { private int id; private double balance; private double annualInterestRate; private java.util.Date dateCreated; public Account() { dateCreated = new java.util.Date(); } public Account(int id, double balance) { this(); this.id = id; this.balance = balance; } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setBalance(double balance) { this.balance = balance; } public double getBalance() { return balance; } public String getDateCreated() { return this.dateCreated.toString(); } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public double getAnnualInterestRate() { return annualInterestRate; } public double getMonthlyInterestRate() { return (annualInterestRate / 1200 ); } public void withdraw(double amount) { this.balance - = amount; } public void deposit(double amount) { this.balance += amount; } }

b) Write a test class named TestAccount that creates an Account object with

an account ID of 2233, a balance of $40,000 and an annual interest rate of

11.5%. Use the withdraw method to withdraw $8,500, use the deposit

method $12,000, and print the balance, the monthly interest, and the date

when this account was created.

public class TestAccount { public static void main(String[] args) { Account account = new Account( 2233 , 40000 ); account.setAnnualInterestRate(11.5); account.withdraw( 8500 ); account.deposit( 12500 ); System.out.println("Balance: $"+ account.getBalance()); System.out.println("Monthly Interest: "+ account.getMonthlyInterestRate()); System.out.println("Date Created: "+ account.getDateCreated()); } }