0% found this document useful (0 votes)
13 views7 pages

Answers 111

Uploaded by

Zack Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Answers 111

Uploaded by

Zack Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MODIBBO ADAMA UNIVERSITY,

YOLA
DEPARTMENT OF PHYSICS

ASSIGNMENT ON;
Computer Programming I (CSC 201)

I.D NUMBER: PHY/21D/3110

DATE: 27TH MARCH, 2023

1
2
QUESTIONS
1. With examples list and explain the concept of OOP (Object Oriented
Programming) Language.
2. Write a Java program to display; Your Name, Matriculation Number,
Department and with your Level on a sprat line when the code will be writing
in a single line.
3. What are the attributes of Java programming language to other High-Level
Language (HLL)?
4. Discuss extensively what you understand by the Java keyword.
5. Write 10 Java keywords with their syntax and example.
6. Explain Java basic data type with examples.

ANSWERS

QUESTION ONE (1): Concept of Object-Oriented Programming Language;

Concept of Object-Oriented Programming (OOP): is to provide a programming


paradigm that focuses on the use of objects to represent and manipulate data. In OOP, data is
encapsulated within objects, and objects are defined by their properties (attributes) and
behaviors (methods). OOP provides several key concepts that enable developers to write
modular, reusable, and maintainable code.

Java defines OOP concepts as follows:

1. Encapsulation: Encapsulation is a protective barrier that keeps the data and code safe
within the class itself. We can then reuse objects like code components or variables
without allowing open access to the data system-wide. This is the practice of keeping
fields within a class private, then providing access to those fields via public methods.
Example of Encapsulation in OOP is a class called `car`, A `car`, object can have a
set of attributes such as 'make, model, year, and `mileave`. We don’t want other
classes to be able to access or modify these attributes directly, so we encapsulate them
by making them private or protected. We provide public methods, such as `get–
make()`, `get Year()` and `getMileage ()` to access the value of these attributes
2. Inheritance: A special feature of Object-Oriented Programming in Java, Inheritance
lets programmers create new classes that share some of the attributes of existing
classes. Using Inheritance lets us build on previous work without reinventing the
wheel. Example of Inheritance is creating a vehicle like `Car` `

3
Motorcycle`, `Truck` etc. The vehicle class will have common
properties and methods that all types of vehicles have, like ` make`,
` model`, ` year`, getMileage()`, ` accelerate()`, `brake`, and so on.
3. Polymorphism: Allows programmers to use the same word in Java to mean different
things in different contexts. One form of polymorphism is method overloading; that’s
when the code itself implies different meanings. The other form is method overriding;
that’s when the values of the supplied variables imply different meanings. Let’s delve
a little further. Example of Polymorphism is creating a ‘Shape’ class with a
‘calculateArea()` method, and then creating multiple subclasses of
‘Shape’ like `Circle`, `Square` and `Triangle`. Each sub-class will
implement the `calculateArea()’ method in a unique way that is
appropriate for that specific shape.
4. Abstraction: In Java, abstraction means simple things like objects, classes and
variables represent more complex underlying code and data. This is important
because it lets you avoid repeating the same work multiple times. Examples of
Abstraction in OOP is OOP is creating an abstract class `Animal’ which
contains common properties and methods that all animals have, but
doesn’t provide any implementation for those methods. Instead, the
implementation will be provided by each subclass of `Animal’.

QUESTION TWO (2): A Java Program;

```
Public class MyInfo {
Public static void main (string[]arg) {
//Input
System.out.print (“Zack Yusuf”,);
Name = input.nextline();
System.out.print (PHY/21D/3110)
Matric Number = input.nextline();
System.out.print (“PHYSICS”,);
Department = input.nextline();
System.out.printIn (“200L”,);
Level = input.nextline();
}
}

```
//Output
System.out.printin(“Name:”+Matric Number:”+Department:”+Level);}
```
QUESTION THREE (3): Attributes of Java to other High-Level Language;

4
1. Simple: Java is a simple programming language and easy to understand because it
does not contain complexities that exist in prior programming languages. In fact,
simplicity was the design aim of Java soft people, because it has to work on electronic
devices where less memory/resources are available. Java contains the same syntax as
C, and C++, so the programmers who are switching to Java will not face any
problems in terms of syntax. Secondly, the concept of pointers has been completely
removed from Java which leads to confusion for a programmer and pointers are also
vulnerable to security.
2. Object-Oriented: Java is an Object-Oriented Programming Language, which means
in Java everything is written in terms of classes and objects. Now, what is an Object?
The object is nothing but a real-world entity that can represent any person, place, or
thing and can be distinguished from others. Every object near us has some state and
behavior associated with it.
3. Platform Independent: The design objective of java soft people is to develop a
language that must work on any platform. Here platform means a type of operating
system and hardware technology. Java allows programmers to write their program on
any machine with any configuration and to execute it on any other machine having
different configurations.
4. Portable: The WORA (Write Once Run Anywhere) concept and platform-
independent feature make Java portable. Now using the Java programming language,
developers can yield the same result on any machine, by writing code only once. The
reason behind this is JVM and bytecode. Suppose you wrote any code in Java, then
that code is first converted to equivalent bytecode which is only readable by JVM. We
have different versions of JVM for different platforms. Windows machines have their
own version of JVM, Linux has its own and macOS has its own version of JVM. So if
you distribute your bytecode to any machine, the JVM of that machine would
translate the bytecode into the respective machine code. In this way portability lets the
programmers focus on development and productivity rather than writing different
code for different platforms.
5. Robust: The Java Programming language is robust, which means it is capable of
handling unexpected termination of a program. There are 2 reasons behind this, first,
it has a most important and helpful feature called Exception Handling. If an exception
occurs in java code then no harm will happen whereas, in other low-level languages,
the program will crash.
6. Secure: In today’s era, security is a major concern of every application. As of now,
every device is connected to each other using the internet and this opens up the
possibility of hacking. And our application built using java also needs some sort of
security. So Java also provides security features to the programmers. Security
problems like virus threats, tampering, eavesdropping, and impersonation can be
handled or minimized using Java. Encryption and Decryption feature to secure your
data from eavesdropping and tampering over the internet. An Impersonation is an act
of pretending to be another person on the internet.
7. Interpreted: In programming languages, you have learned that they use either the
compiler or an interpreter, but Java programming language uses both a compiler and
an interpreter. Java programs are compiled to generate bytecode files then JVM
interprets the bytecode file during execution. Along with this JVM also uses a JIT
compiler (it increases the speed of execution).
8. Multi-Threaded: Thread is a lightweight and independent subprocess of a running
program (i.e, process) that shares resources. And when multiple threads run
simultaneously is called multithreading. In many applications, you have seen multiple

5
tasks running simultaneously, for example, Google Docs where while typing text, the
spell check and autocorrect tasks are running.

QUESTION FOUR (4): Java Keywords;

Java Keywords: are predefined reserved words, used in Java programming that have
special meanings to the compiler. These keywords cannot be used as identifiers
or variables in a Java program. For example: int score; Here, int is a keyword.

QUESTION FIVE (5): Java Keywords with their Syntax and Examples;

S/N KEYWORDS SYNTAX EXAMPLE


1. Class class [name] Class MyClass { … }

2. Public public Public class HelloWorld


[class/method/variable] {…}
3. Private private Private int variable = 0;
[class/method/variable]
4. Abstract abstract [class/method] Abstract class MyClass
{…}
5. Static static Static int variable = 0;
[class/method/variable]
6. Final final final int variable = 0;
[class/method/variable]
7. Void void [method] Void method() { … }

8. Interface interface [name] interface MyInterface { …


}
9. New new [object] new MyObject();

10. Protected protected


[class/method/variable]

QUESTION SIX (6): Java Basic Data Types;

Data Types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:

1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays

JAVA PRIMITIVE DATA TYPES

In Java language, primitive data types are the building blocks of data manipulation. These are
the most basic data types available in Java language.

6
Java is a statically-typed programming language. It means, all variables must be declared
before its use. That is why we need to declare variable's type and name.

There are 8 types of primitive data types:

1. Boolean Data Type: The Boolean data type is used to store only two possible values:
true and false. This data type is used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be defined
precisely. Example: Boolean one = false
2. Byte Data Type: It is an 8-bit signed two's complement integer. Its value-range lies
between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is
127. Its default value is 0. The byte data type is used to save memory in large arrays
where the memory savings is most required. It saves space because a byte is 4 times
smaller than an integer. It can also be used in place of "int" data type. Example:
byte a = 10, byte b = -20
3. Short Data Type: The short data type is a 16-bit signed two's complement integer. Its
value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768
and maximum value is 32,767. Its default value is 0. The short data type can also be used
to save memory just like byte data type. A short data type is 2 times smaller than an integer.
Example: short s = 10000, short r = -5000
4. Int Data Type: The int data type is a 32-bit signed two's complement integer. Its
value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1)
(inclusive). Its minimum value is - 2,147,483,648and maximum value is
2,147,483,647. Its default value is 0. The int data type is generally used as a default
data type for integral values unless if there is no problem about memory. Example:
int a = 100000, int b = -200000
5. Long Data Type: The long data type is a 64-bit two's complement integer. Its value-range
lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)
(inclusive). Its minimum value is - 9,223,372,036,854,775,808and maximum value is
9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a
range of values more than those provided by int. Example: long a = 100000L, long b = -
200000L
6. Float Data Type: The float data type is a single-precision 32-bit IEEE 754 floating
point. It’s value range is unlimited. It is recommended to use a float (instead of
double) if you need to save memory in large arrays of floating point numbers. The
float data type should never be used for precise values, such as currency. Its default
value is 0.0F. Example: float f1 = 234.5f
7. Double Data Type: The double data type is a double-precision 64-bit IEEE 754
floating point. Its value range is unlimited. The double data type is generally used for
decimal values just like float. The double data type also should never be used for
precise values, such as currency. Its default value is 0.0d. Example: double d1 = 12.3
8. Char Data Type: The char data type is a single 16-bit Unicode character. Its value-
range lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is
used to store characters. Example: char letterA = 'A'

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy