Cosc Final Assignment
Cosc Final Assignment
array elements.
int sum = 0;
return average;
}
}
if (containsValue) {
System.out.println("The array contains the value: " + valueToFind);
} else {
System.out.println("The array does not contain the value: " + valueToFind);
}
}
return false;
}
}
int[] numbers = {12, 34, 56, 78, 910, 112, 1314, 1516, 1718};
return -1;
}
}
if (isInteger) {
System.out.println("The number " + number + " is an integer.");
} else {
System.out.println("The number " + number + " is not an integer.");
}
}
import java.util.Scanner;
return smallest;
}
}
Question 7) Write a Java method to display the middle character of
a string. Note: a) If the length of the string is odd there will be two
middle characters. b) If the length of the string is even there will be
one middle character. Sample Data: • Input a string: 350 • Expected
Output: o The middle character in the string: 5 • Input a string:
3501 • Expected Output: o The middle character in the string: 50
import java.util.Scanner;
if (length % 2 == 0) {
return str.substring(middle - 1, middle + 1);
} else {
return str.substring(middle, middle + 1);
}
}
}
if (number > 0) {
System.out.println("Number is positive");
} else if (number < 0) {
System.out.println("Number is negative");
} else {
System.out.println("Number is zero");
}
}
}
import java.util.Scanner;
return greatest;
}
}
Question 10) Write a Java program to print the results of the
following operations. Test Data: a. -5 + 8 * 6 b. (55+9) % 9 c. 20 + -
3*5 / 8 d. 5 + 15 / 3 * 2 - 8 % 3
int result1 = -5 + 8 * 6;
int result2 = (55 + 9) % 9;
int result3 = 20 + -3 * 5 / 8;
int result4 = 5 + 15 / 3 * 2 - 8 % 3;
System.out.println("a. -5 + 8 * 6 = " + result1);
System.out.println("b. (55 + 9) % 9 = " + result2);
System.out.println("c. 20 + -3 * 5 / 8 = " + result3);
System.out.println("d. 5 + 15 / 3 * 2 - 8 % 3 = " + result4);
}
}
Parameterized Constructor
3. Multilevel Inheritance: A class inherits from another class, which itself is a subclass of
another class, creating a chain of inheritance.
4. Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
5. Hybrid Inheritance: A combination of two or more types of inheritance. Since Java does
not support multiple inheritance with classes directly, hybrid inheritance is usually
implemented using interfaces.
Question 13) what is a class? How does it serve as a blueprint for
objects, and how objects are instances of classes?
A class specifies what data (properties) and behaviors (methods) the objects created from it will
have. For example, if you have a car class, it might define attributes like color and make, and
methods like drive() and brake().
An object is an instance of a class. When you create an object from a class, you are creating a
specific instance of that class with its own set of attribute values. For example, you might create
a car object called mycar with the color set to zblack and the make set to BMW. Mycar is an
instance of the car class.
1) Encapsulation:
Encapsulation involves bundling data (attributes) and methods (functions) into a single unit,
known as a class. It restricts direct access to some components, which helps protect the object's
state and ensures that data is accessed and modified only through defined methods.
2) Inheritance:
Inheritance allows a new class (subclass) to inherit characteristics and behaviors from an existing
class (superclass). This promotes code reuse and establishes a hierarchical relationship between
classes, where the subclass inherits the properties and methods of the superclass.
3) Polymorphism:
Polymorphism enables objects to be treated as instances of their parent class rather than their
actual class. It allows methods to perform different tasks based on the object’s actual class type,
even though they share the same name.
4) Abstraction:
Abstraction involves hiding the complex implementation details and exposing only the necessary
features of an object. It simplifies interactions with objects by focusing on high-level
functionality, rather than on the intricate details of the implementation.
Question 17) what are the various types of variables and explain
each type.
The various types of variables are explained below;
1) Local Variables:
Local variables are declared within a method, constructor, or block and are only accessible
within that method, constructor, or block. They are created when the method or block is executed
and destroyed when it finishes. They are Limited to the method or block where they are declared.
They Must be initialized before use.
2) Instance Variables:
Instance variables are declared within a class but outside any method. They are associated with a
particular instance of a class and represent the state of the object. They are accessible to all
methods and constructors in the class. Each object of the class has its own copy of instance
variables.
Class variables are declared with the static keyword within a class. They are shared among all
instances of the class and belong to the class itself rather than to any particular instance. They are
accessible to all methods, constructors, and static blocks in the class. There is only one copy of a
static variable, regardless of the number of instances
Question 18) What is the Java Virtual Machine (JVM) and why is it
important?
The Java Virtual Machine (JVM) is a critical component of the Java programming environment.
It is an abstract computing machine that enables Java programs to run on any device or operating
system that has a JVM implementation. The JVM is important because of the following points
presented below;
1) Allows Java programs to run on any device or operating system with a JVM, enabling the
"write once, run anywhere" capability.
2) memory allocation and deallocation through automatic garbage collection, reducing the
risk of memory leaks and optimizing resource use.
3) Provides a secure execution environment with features like bytecode verification and a
security manager to prevent the execution of malicious code.
4) Includes a Just-In-Time (JIT) compiler that translates bytecode into native machine code
at runtime, enhancing the performance of Java applications.
5) Supports a robust exception handling mechanism to manage and respond to runtime
errors effectively, ensuring program stability.
Ques&on 19) Case Diagram