Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8). Name five of the fundamental ter ns which encompass object-oriented programm

ID: 3606515 • Letter: 8

Question

8). Name five of the fundamental ter ns which encompass object-oriented programming 9). Write a class called NumberOfGoals that represents the total number of goals scored by a ootball team. The NumberOfGioals class should contain a single integer as data, representing the number of goals scored. Write a constructor to initialize the number of goals to Zero. 10). Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the variable x and prompt the user for a float value and input it using the Scanner class into the variable y 11). Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include Boolean variable called occupied as data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant ( the constructor should initialize occupied to false. 12) Provide three examples of code using assignment statements where one assignment statement would result in a syntax error, one would result in a logical error, and one would result in a run-time error

Explanation / Answer

8. Five fundamental terms associated with object-oriented programming are -

a)inheritance - getting the property of a parent class in a base class

b)polymorphism - to exist in more than one form

c)encapsulation - wrapping of text and data into a single unit

d)abstraction - to show only the least amount of data that is required by the user

e)classes - an collection of objects

9.

The code for the following question is as follows:

pulbic class NumberOfGoals

{

int goals;

public NumberOfGoals()

{

goals=0;

}

public static void main(String args[])

{

//rest of the code in here

}

}

In here NumberOfGoals in the class name followed by the integer goals.When we call the constructor NumberOfGoals() it is automatically initialized to 0;

10.The code for the following question in Java is as follows

import java.util.*;

public class input

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

int x = sc.nextInt();

float y= sc.nextFloat();

}

}

Here we import the scanner class by callin import over java.util,and use an object of the Scanner class namely sc to call the nextInt and nextFloat functions for keyboard input and storing the data in x and y respectively.

11.The code for the following question is as follows

import java.util.*;

public class Shelf

{

int length;

int bredth;

boolean capacity;

public Shelf(int length,int bredth)

{

this.length=length;

this.bredth=bredth;

capacity=false;

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

Shelf ob=new Shelf(sc.nextInt(),sc.nextInt());

}

}

Here we are having 3 global variables namely length,bredth and capacity.However Since we need to use a constructor to initialize their values,we use the this keyword to initialize the global values of the variables with the local parametric value.However to call a constructor an object needs to be created,as such we create an object of of type Shelf,and the values in it are passed from the Scanner object's call to nextInt().

12. Example of an syntax error is as follows :

class example

{

pub static void main(String args[])

{

}

}

Here we have written 'pub' instead of public as a result error is throws.

Example of logical error is as follows :

public class Example
{
public static void main(String[] args)
{
int k;
for (k=1; k<=10; k++) ;
{
System.out.println("Count is " + k);
}
}
}

Here a semicolon is present after the for loop as such it shows itself as an empty loop and its body is treated as a seperate block of code

Example of runtime error is :

public class Example
{
public static void main(String[] args)
{
int x=0;
int y=100;
int div=y/x;
}
}

Since we are dividing 100 by 0 an divide by zero exception is thrown at runtime.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote