School of Informatics and Electrical Engineering: Hachalu Hundessa Campus

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Ambo University

Hachalu Hundessa Campus

School of Informatics and Electrical

Engineering

Department of Computer Science

Object Oriented Programming individual

Project

Name: Mulualem Efa

ID.No: BET/1259/11

Submitted date: May, 2021


1. List and discuss the environment on which you can type java Programming

Answer:

Programming environments can be divided into two very different types: integrated


development environments and command-line environments. All programming environments
for Java require some text editing capability, a Java compiler, and a way to run applets and
stand-alone applications. An integrated development environment, or IDE, is a graphical user
interface program that integrates all these aspects of programming and probably others (such
as a debugger, a visual interface builder, and project management). A command-line
environment is just a collection of commands that can be typed in to edit files, compile
source code, and run programs.

JRE Components

The JRE is the software environment in which programs compiled for a typical JVM
implementation can run. The runtime system includes:

 Code necessary to run Java programs, dynamically link native methods, manage
memory, and handle exceptions
 Implementation of the JVM

The following figure shows the JRE and its components, including a typical JVM
implementation's various modules and its functional position with respect to the JRE and
class libraries.

JVM

The JVM is an abstract computing machine, having an instruction set that uses memory.
Virtual machines are often used to implement a programming language. The JVM is the
cornerstone of the Java programming language. It is responsible for Java's cross-platform
portability and the small size of its compiled code.

The Solaris JVM is used to execute Java applications. The Java compiler, javac, outputs byte
codes and puts them into a .class file. The JVM then interprets these bytecodes, which can
then be executed by any JVM implementation, thus providing Java's cross-platform
portability. The next two figures illustrate the traditional compile-time environment and the
new portable Java compile-time environment.

Traditional Compile-Time Environment

New Portable Java Compile-Time Environment

Sun Just-In-Time (JIT) Compiler

The Sun Java JIT compiler, an integral part of the Solaris JVM, can accelerate execution
performance many times over previous levels. Long-running, compute-intensive programs
show the best performance improvement.

JIT Compile Process

When the JIT compiler environment variable is on (the default), the JVM reads the .class file
for interpretation and passes it to the JIT compiler. The JIT compiler then compiles the
bytecodes into native code for the platform on which it is running. The next figure illustrates
the JIT compile process.

2. Which of the following Java variable declarations has an error?


A. int x = 5;

B. double temperature = 75.6;

C. char grade = ’A’;

D. String name = ’Adam’;

Answer: D

3. Describe the differences between a compiler, a JIT compiler, and an interpreter.


Answer:
JIT vs. Interpreter
The Just In Time compilation (also known as dynamic translation or JIT) is a technique used
in computing to improve the quality of the runtime performance of a computer program. It is
the amalgamation of two ideas found in runtime environments: Byte code compilation and
dynamic compilation (which is a process that some programming language implementations
use in order to gain performance while a system is executing an action).

An interpreter most accurately describes the execution of an action through a computer


program. There are a few variations on the type of actions an interpreter actually executes: It
directly executes the source code of a program; it translates the source code into a
representation that is an efficient intermediate representation, and then executes the said
code; it executes precompiled code that has been stored and created by a compiler that is part
of the interpreter system.

JIT has the capability to combine the advantages found both in interpretation and static (that
is to say ahead of time) compilation. As an interpreter, JIT is able to improve performance
through caching results of blocks of code that has been translated – compared to simply re-
evaluating every line or operand in the code each time that it occurs (as in interpreted
language). Just like static compiling code at the time of development, JIT is able to recompile
the code if this is found to be the most advantageous plan of action. Also, in the same vein as
static compilation, JIT is capable of enforcing security guarantees. Just like compilers,
interpreters have the capacity to translate code. Both are the primary methods of
implementing programming languages; however, the categories of ‘compiler’ or ‘interpreter’
are not distinct (for their dual roles as code translators). The most obvious disadvantage to
using an interpreter is that once the code is interpreted, the program will inevitably run slower
than when simply compiling the code; however, it takes much less time to interpret coding
than it does to compile and run it (especially pertinent when prototyping and testing code).

Generally speaking, JIT provides much better performance than interpreters, and, in many
cases, provides much better performance than static compilers. Its superiority over JIT,
however, does not bar it from having some major disadvantages: There is a slight delay when
initially executing an application (a side effect of taking time to load and compile byte code).
It will eventually generate better coding; however, the initial delay inherent in doing so will
increase with the quality of coding.

4. Define an integer variable called bank Balance. Initialize it to a value of 500. Then add
250 to it. Then subtract 100 from it. Finally, print the resulting value.
Answer:
int bankBalance = 500;
bankBalance = bankBalance + 250;
bankBalance = bankBalance - 100;
System.out.println(bankBalance);

5. Write a program that asks the user a “what-is-your-favorite” question and then displays a
nice (or a nasty) comment that incorporates the user’s answer. For example (user input is
shown in bold):
What is your favorite movie? Titanic
I think Titanic is a terrible movie!
Just kidding! I like Titanic, too.
6. Write a program that prompts the user to enter an integer and displays the entered value
times two as follows:
Enter an integer: 5
2 * 5 = 10

7. What value for register will be printed at the end of this block of Java code?

double register = 10.0;

register = register + 5; //Customer pays $5.

register = register - 2.5; //Customer receives $2.50 as change.

register = register + 10; //Customer pays $10. register = register - 3; //Customer receives $3
as change. System.out.println(register);

A. 19.0 D. 25.5

B. 19.5 Answer: B

C. 22.5
8. Define an integer variable called bankBalance. Initialize it to a value of 500. Then add
250 to it. Then subtract 100 from it. Finally, print the resulting value.

Answer:

int bankBalance = 500;

bankBalance = bankBalance + 250;

bankBalance = bankBalance - 100;

System.out.println(bankBalance);

9. What value will be printed by this line of Java code? System.out.println(2.0 * (5 / 2));
A. 4 B. 4.0 C. 5 D. 5.0 E. This line of code will give an error.

Answer: B

10. Write Java code to define an integer variable called day, and a String variable called
month. Give month and day appropriate values for your birthday.

Answer:

int day;

String month;

day = 3;

month = "July";

//Note that this can be compressed into two lines by defining the

//variables and giving them an initial value at the same time:

int day = 3;

String month = "July";

11. Write the java program that take input from keyboard and calculate square root of any
integer number
12. Write Java code to create a String variable called first Name, define it to be your first
name as a String. Then define a variable called last Name and define it to be your last
name as a String. Then define a variable called full Name and set it to be your first name
followed by a space followed by your last name. Use the existing variables for your first
and last name and String concatenation to define full Name. Finally, write code to print
this text: Hello, my name is [full name]. There are [number] letters in my name. Use
String concatenation to create the first String to print using the full Name variable, and
use the. length () command on first Name and last Name to calculate the number of
letters. Note: you can concatenate an integer and a String and the integer will be
converted to a String. For example, this expression: "There are "+ 7 + "days in a week."
will be evaluated as the String: "There are 7 days in a week."

Answer:

String firstName = "MULUALEM";


String lastName = "EFA";
String fullName = firstName + " " + lastName;
System.out.println("Hello, my name is " + fullName + ".");
int lettersInName = firstName.length() + lastName.length();
System.out.println("There are " + lettersInName
+ " letters in my name.");
13. Write a program in Java to display the cube of the number up to given an integer
import java.util.*;
import java.io.*;
import java.lang.*;
public class pub {

public static void main(String[] args)


{
Scanner s=new Scanner(System.in);
double a,b,c;
a=s.nextDouble();
for(int i=0;i<a;i++)
{
System.out.println("CUBE OF "+i+" is "+i*i*i);
}
}

}
14. Write a program in Java to display the multiplication table of a given integer.
TestData
Input the number (Table to be calculated): Input number of terms: 5
Expected Output:
5X0=0
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
import java.util.Scanner;
public class Exercise14 {

public static void main(String[] args)

{
int j,n;

System.out.print("Input the number(Table to be calculated): ");


{
System.out.print("Input number of terms : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();

System.out.println ("\n");
for(j=0;j<=n;j++)

System.out.println(n+" X "+j+" = " +n*j);


}
}
}
15. Write a program in Java to display the pattern like a diamond. 

TestData
Input number of rows (half of the diamond): 7
Expected Output:

*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
16. Write a Java function named calculateTip(). The access modifier should be public, it
should have a return type of double, and it should take as input a double parameter
representing the cost of a meal at a restaurant. And finally, it should return a double equal
to 15% of the cost parameter.
Answer:

public double calculateTip(double cost) {


double tip = cost * 0.15;
return tip;
}

You might also like