
Core Java Programming Book
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
All prices
More details
Content
Chapter 1 Data type and variables
1. Write a Java Program that declares and initializes variables of different data types (int, double, boolean, String)
and prints their values. Ensure that the program demonstrates the use of appropriate data types.
Code-
class VariableDemo {
public static void main(String[] args) {
// Declare and initialize variables of different data types
int integerVariable = 40;
double doubleVariable = 3.14;
boolean booleanVariable = false;
String stringVariable = "Hello, World!";
// Print the values of the variables
System.out.println("Integer Variable: " + integerVariable);
System.out.println("Double Variable: " + doubleVariable);
System.out.println("Boolean Variable: " + booleanVariable);
System.out.println("String Variable: " + stringVariable);
}
}
Output-
Integer Variable: 40
Double Variable: 3.14
Boolean Variable: false
String Variable: Hello, World!
2. Create a Java Program that converts a given double value into an integer by performing explicit type casting.
Print both the original double value and the converted integer value.
Code-
public class DoubleToIntConversion {
public static void main(String[] args) {
// Given double value
double doubleValue = 7.76;
// Convert the double value to an integer using explicit casting
int intValue = (int) doubleValue;
// Print the original double value and the converted integer value
System.out.println("Original Double Value: " + doubleValue);
System.out.println("Converted Integer Value: " + intValue);
}
}
Output-
Original Double Value: 7.76
Converted Integer Value: 7
3. Define a constant named "PI" with a value of 3.14159 in a Java Program. Calculate and print the area of a
circle with a radius of 5 using this constant.
Code-
public class CircleArea {
public static final double PI = 3.14159;
public static void main(String[] args) {
// Given radius of the circle
double radius = 5.0;
// Calculate the area of the circle using the constant PI
double area = PI * radius * radius;
// Print the calculated area
System.out.println("The area of the circle with a radius of " + radius + " is: " + area);
}
}
Output-
The area of the circle with a radius of 5.0 is: 78.53975
4. Create a Java Program that prompts the user to enter their full name as a single String. Split the name into first
name and last name, and then print both names separately.
Code-
import java.util.Scanner;
public class NameSplitter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter their full name
System.out.print("Enter your full name: ");
String fullName = scanner.nextLine();
// Split the full name into first name and last name
String[] nameParts = fullName.split(" ");
// Check if there are at least two parts (first name and last name)
if (nameParts.length >= 2) {
String firstName = nameParts[0];
String lastName = nameParts[nameParts.length - 1];
// Print the first name and last name separately
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
} else {
System.out.println("Invalid input. Please enter your full name with at least a first name and last name.");
}
scanner.close();
}
}
Output-
Enter your full name: Jayant Sharma
First Name: Jayant
Last Name: Sharma
5. Create a Java Program that calculates the sum of two integer variables.
Code-
public class IntegerVariables {
public static void main(String[] args) {
int num1 = 300;
int num2 = 400;
int sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
Output-
Sum: 700
6. Create a Java Program that calculates the product of two Floating-Point variables.
Code-
public class FloatingPointVariables {
public static void main(String[] args) {
double num1 = 3.14;
double num2 = 1.66;
double product = num1 * num2;
System.out.println("Product: " + product);
}
}
Output-
Product: 5.2124
7. Create a Java Program that declares and prints a character variable.
Code-
public class CharacterVariable {
public static void main(String[] args) {
char grade = 'A';
System.out.println("Grade: " + grade);
}
}
Output-
Grade: A
8. Create a Java Program that declares and prints a string variable.
Code-
public class StringVariable {
public static void main(String[] args) {
String greeting = "Hello, Java!";
System.out.println(greeting);
}
}
Output-
Hello, Java!
9. Create a Java Program that declares and prints a boolean variable.
Code-
public class BooleanVariable {
public static void main(String[] args) {
boolean isJavaFun = false;
System.out.println("Is Java fun? " + isJavaFun);
}
}
Output-
Is Java fun? false
10. Create a Java Program that converts an integer value to a double value and prints the result.
Code-
public class TypeConversion {
public static void main(String[] args) {
int intValue = 52;
double doubleValue = (double) intValue;
System.out.println("Double Value: " + doubleValue);
}
}
Output-
Double Value: 52.0
11. Write a Java Program to demonstrate explicit narrowing conversion.
Code-
public class NarrowingConversion {
public static void main(String[] args) {
double doubleValue = 45.67;
int intValue = (int) doubleValue;
System.out.println("Int Value: " + intValue);
}
}
Output-
Int Value: 45
12. Write a Java Program to demonstrate mixing data types.
Code-
public class MixingDataTypes {
public static void main(String[] args) {
int intValue = 50;
char charValue = (char) intValue;
System.out.println("Char Value: " + charValue);
}
}
Output-
Char Value:2
13. Write a Java Program to demonstrate numeric promotion.
Code-
public class NumericPromotion {
public static void main(String[] args) {
byte byteValue = 10;
int intValue = 20;
short shortValue = 30;
int result = byteValue + intValue + shortValue;
System.out.println("Result: " + result);
}
}
Output-
Result: 60
14. Write a Java program to demonstrate string to integer conversion.
Code-
public class StringToIntegerConversion {
public static void main(String[] args) {
String numString = "12345";
int intValue = Integer.parseInt(numString);
System.out.println("Integer Value: " + intValue);
}
}
Output-
Integer Value: 12345
15. Write a Java program to demonstrate character to integer conversion.
Code-
public class CharToIntegerConversion {
public static void main(String[] args) {
char charValue = '9';
int intValue = Character.getNumericValue(charValue);
System.out.println("Integer Value: " + intValue);
}
}
Output-
Integer Value: 9
Practise Questions
1. Write a Java Program that declares and initializes variables of different data types (int, double, char, boolean), and then print their values to the console.
2. Create a Java Program that demonstrates type conversion (casting) between different data types, including implicit and explicit conversions. Print the results of these conversions.
3. Write a Java Program that takes two integers as input from the user and performs arithmetic operations (addition, subtraction, multiplication, division) on them. Display the results with appropriate data type handling.
4. Develop a Java Program that prompts the user to enter a sentence as a string. Then, count and display the number of characters, words, and vowels in the sentence.
5. Write a program to print the area of a rectangle of sides 2 and 3 units...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.