0% found this document useful (0 votes)
98 views8 pages

Core Java Exercises Section1:Basics

This document provides examples of Java programming exercises covering core Java concepts like operators, expressions, conditionals, loops, arrays, strings, object-oriented programming, exceptions and more. It includes 25 sections with examples of writing Java code to calculate mathematical expressions, find prime numbers in a range, sort arrays, manipulate strings, create classes for employees and books with getter/setter methods, implement inheritance between vehicle classes like cars and trucks, and perform CRUD operations on employee data using exceptions and logs.

Uploaded by

Vamsi Vardhneni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
98 views8 pages

Core Java Exercises Section1:Basics

This document provides examples of Java programming exercises covering core Java concepts like operators, expressions, conditionals, loops, arrays, strings, object-oriented programming, exceptions and more. It includes 25 sections with examples of writing Java code to calculate mathematical expressions, find prime numbers in a range, sort arrays, manipulate strings, create classes for employees and books with getter/setter methods, implement inheritance between vehicle classes like cars and trucks, and perform CRUD operations on employee data using exceptions and logs.

Uploaded by

Vamsi Vardhneni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Core Java Exercises

==========================================================
Section1 :Basics
==========================================================
1. Write a Java program to print the result of the following operations.

a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3

Expected Output:
43
1
19
13

2. Write a Java program to find the value of specified expression.

a) 101 + 0) / 3
b) 3.0e-6 * 10000000.1
c) true && true
d) false && true
e) (false && false) || (true && true)
f) (false || false) && (true && true)

Expected Output:
(101 + 0) / 3)-> 33
(3.0e-6 * 10000000.1)-> 30.0000003
(true && true)-> true
(false && true)-> false
((false && false) || (true && true))-> true
(false || false) && (true && true)-> false

3. Write a Java program to compute a specified formula

Specified Formula :
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))

Expected Output
2.9760461760461765

4. Write a Java program to print the area and perimeter of a circle.


Test Data:
Radius = 7.5
Expected Output
Perimeter is = 47.12388980384689
Area is = 176.71458676442586

5.Write a Java program to compute body mass index (BMI).


BMI: The BMI is defined as the body mass divided by the square of the body height, and is
universally expressed in units of kg/m2, resulting from mass in kilograms and height in metres.

Test Data
Input weight in pounds: 452
Input height in inches: 72

Expected Output

Body Mass Index is 61.30159143458721


==================================================================
Section 2: Conditionals
==================================================================

6. Write a Java program to solve quadratic equations (use if, else if and else)
Test Data
Input a: 1
Input b: 5
Input c: 1
Expected Output:
The roots are -0.20871215252208009 and -4.7912878474779195

7.Write a Java program that accepts two floating point numbers and checks whether they are the
same up to two decimal places.

Test Data
Input first floating point number:2.585
Input second floating point number: 2589

Expected Output:

Numbers are same


8. A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
Test Data : 47
Expected Output: Grade D
=======================================================================
Section 3: Loops and functions
======================================================================
9.Define a method to read two intergers x and y and find all prime numbers in the range

10.A three digit number is called Armstrong number if sum of cube of its digit is equal to number
itself.
E.g.- 153 is an Armstrong number because (13)+(53)+(33) = 153.
Write all Armstrong numbers between 100 to 500.

11.Write a Java program to sort a numeric array and a string array


12..Write a Java program to find the index of an array element
13.Write a Java program to remove an array element
14.Write a Java program to insert an element (specific position) into an array.

15.Write a Java program to reverse an array of integer values.


=======================================================================
Section 4: Strings
=======================================================================
16.Write a Java program to concatenate a given string to the end of another string.
17.Take 2 strings as input and find lexicographically smaller string.
18.Perform String sorting on an array.
=====================================================================
Section 5: OOPS
=====================================================================
19.Create a class called Employee that includes three pieces of information as instance variables—a
first name (String), a last name (String) and a monthly salary (double). Your class should have a
constructor that initializes the three instance variables. Provide a set and a get method for each
instance variable.
If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that
demonstrates class Employee’s capabilities.
Create two Employee objects and display each object’s yearly salary. Then give each Employee a
10% raise and display each Employee’s yearly salary again.
20.Create a class called Book to represent a book. A Book should include four pieces of information
as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class
should have a constructor that initializes the four instance variables.
Provide a mutator method and accessor method (query method) for each instance variable.
Inaddition, provide a method named getBookInfo that returns the description of the book as a String
(the description should include all the information about the book). You should use this keyword in
member methods and constructor. Write a test application named BookTest to create an array of
object for 30 elements for class Book to demonstrate the class Book's capabilities
21
i. Create a super class called Car. The Car class has the following fields and methods. ◦intspeed;
◦doubleregularPrice; ◦Stringcolor;
ii. Create a sub class of Car class and name it as Truck.
The Truck class has the following fields and methods.
◦intweight; ◦doublegetSalePrice();//Ifweight>2000,10%discount.Otherwise,20%discount
iii. Create a subclass of Car class and name it as Ford. The Ford class has the following fields
and methods
◦int year; ◦double manufacturerDiscount; ◦
doublegetSalePrice();//FromthesalepricecomputedfromCarclass,subtractthemanufacturerDiscount
iv. Create a subclass of Car class and name it as Sedan. The Sedan class has the following fields
and methods. ◦intlength;
◦doublegetSalePrice();//Iflength>20feet,5%discount,Otherwise,10%discount
v. Create MyOwnAutoShop class which contains the main() method. Perform the following within
the main() method.
(a) Create an instance of Sedan class and initialize all the fields with appropriate values. Use
super(...) method in the constructor for initializing the fields of the superclass.
(b) Create two instances of the Ford class and initialize all the fields with appropriate values. Use
super(...) method in the constructor for initializing the fields of the super class.
(c) Create an instance of Car class and initialize all the fields with appropriate values. Display the
sale prices of all instance.
22.Write a discount system for a beauty saloon, which provides services and sells beauty products. It
offers 3 types of memberships: Premium, Gold and Silver. Premium, gold and silver members
receive a discount of 20%, 15%, and 10%, respectively, for all services provided. Customers without
membership receive no discount. All members receives a flat 10% discount on products purchased
(this might change in future).
Your system shall consist of four classes:Customer, Discount, Visit and BeautySaloon, as shown
in the class diagram. It shall compute the total bill if a customer purchases $x of products and $y of
services, for a visit. Class named BeautySaloon implements interface IServiceProvider interface and
holds list of Customers.
Interface IserviceProvider{
public double calculateBill(String name,Date date);
}

23.Read employee database and write it to excel.


24.Perform CRUD operations on Employee data .
25.Add appropriate user defined exceptions in the above question and use logs wherever necessary

You might also like