0% found this document useful (0 votes)
15 views9 pages

Java Programming Exercises and Solutions

Java programs 1

Uploaded by

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

Java Programming Exercises and Solutions

Java programs 1

Uploaded by

nagpalmokesh18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Roll No.

- MCA/10071/24
Date – 8/13/2024
Q1> Write a program to print your name, roll no, section and branch
in separate lines

Q2> Write a program in Java to take first name and last name from the user and
print both in one line as last name followed by first name.

Q3> Make a program to make a multiplication table as shown


Q4> Write a program to calculate area for circle. Take input at run time.
Q5> Write a program to calculate area for square. Take input at run time.
Q6> Write a program to calculate area for triangle. Take input at run time.
Q7> Write a program to calculate area for rectangle. Take input at run time.
Bonus 1> Write a program to input a number and find its 1's complement and 2's
complement of the number.

Bonus 2> Write a program to input a decimal number and find its hexadecimal
value.

Bonus 3> Write a program to find the bytes of all the primitive datatypes.

Q1 - Write a program to print your name, roll no, section and branch
in separate lines
/*
8/13/2024
Q> Write a program to print your name, roll no, section and branch
in separate lines **/
import [Link].*;

public class Main {


public static void main(String[] args) {
String name = "",roll_no = "", section = "", branch ="";
Scanner sc = new Scanner([Link]);
[Link]("Input your name: ");
name = [Link]();

[Link]("Input your roll number: ");


roll_no = [Link]();

[Link]("Input your section: ");


section = [Link]();

[Link]("Input your branch: ");


branch = [Link]();

[Link]("Your details are as follows: ");


[Link]("Name: " + name);
[Link]("Roll. No.: " + roll_no);
[Link]("Section: " + section);
[Link]("Branch: " + branch);
}
}

OUTPUT

Q2 - Write a program in Java to take first name and last name from the user and
print both in one line as last name followed by first name.
import [Link];

/*
8/13/2024
Write a program in Java to take first name and last name from the user and
print both in one line as
last name followed by first name.
*/
public class Main {
public static void main(String[] args) {
String first = "",last = "";
Scanner sc = new Scanner([Link]);
[Link]("Input your first name: ");
first = [Link]();

[Link]("Input your last name: ");


last = [Link]();
[Link]("Your name is as follows: ");
[Link]("Name: " + last + " " + first);
}
}
OUTPUT

Q3 - Make a program to make a multiplication table as shown


/*
8/13/2024
Make a program to make a multiplication table as shown
*/
public class Main {
public static void main(String[] args) {
[Link]("Multiplication table is as follows: ");
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 5; j++) {
[Link](i*j+"\t");
}
[Link]();
}
}
}

OUTPUT
Q4 - Write a program to calculate area for circle. Take input at run time.
/*
8/13/2024
Write a program to calculate area for circle. Take input at run time.
*/

import [Link];
public class Main {
private double area(int a){
return (22*a*a)/7;
}
public static void main(String[] args) {
double result = 0.0;
int a = 0;
Main obj = new Main();
Scanner sc= new Scanner([Link]);
[Link]("Input the radius of the circle: ");
a = [Link]();
result = [Link](a);
[Link]("Area: " + result + " unit(s) sq.");
}
}

OUTPUT

Q5 - Write a program to calculate area for square. Take input at run time.

/*
8/13/2024
Write a program to calculate area for square. Take input at run time.
*/

import [Link];
public class Main {
private double area(int a){
return (a*a);
}
public static void main(String[] args) {
double result = 0.0;
int a = 0;
Main obj = new Main();
Scanner sc= new Scanner([Link]);
[Link]("Input the side of the square: ");
a = [Link]();
result = [Link](a);
[Link]("Area: " + result + " unit(s) sq.");
}
}

OUTPUT

Q6 - Write a program to calculate area for triangle. Take input at run time.
/*
8/13/2024
Write a program to calculate area for triangle. Take input at run time.
*/

import [Link];
public class Main {
private double area(int a, int b, int c){
double s = (a + b + c)/2;
return [Link](s*(s-a)*(s-b)*(s-c));
}
public static void main(String[] args) {
double result = 0.0;
int a = 0, b = 0, c = 0;
Main obj = new Main();
Scanner sc= new Scanner([Link]);
[Link]("Input the first side of the triangle: ");
a = [Link]();
[Link]("Input the second side of the triangle: ");
b = [Link]();
[Link]("Input the third side of the triangle: ");
c = [Link]();
result = [Link](a, b, c);
[Link]("Area: " + result + " unit(s) sq.");
}
}
OUTPUT

Q7 - Write a program to calculate area for rectangle. Take input at run time.
/*
8/13/2024
Write a program to calculate area for rectangle. Take input at run time.
*/

import [Link];
public class Main {
private double area(int a, int b){
return (a * b);
}
public static void main(String[] args) {
double result = 0.0;
int a = 0, b = 0;
Main obj = new Main();
Scanner sc= new Scanner([Link]);
[Link]("Input the length of the rectangle: ");
a = [Link]();
[Link]("Input the breath of the rectangle: ");
b = [Link]();
result = [Link](a, b);
[Link]("Area: " + result + " unit(s) sq.");
}
}

OUTPUT
BONUS 1 - Write a program to input a number and find its 1's complement and
2's complement of the number.
/*
8/13/2024
Write a program to input a number and find its 1's complement and 2's
complement of the number.
*/

import [Link];
public class Main {
private double area(int a){
return (a*a);
}
public static void main(String[] args) {
int a = 0;
String b ="", c = "";
Scanner sc= new Scanner([Link]);
[Link]("Input a number: ");
a = [Link]();
if(a<0){
b = [Link](-a);
while ([Link]() < 32) {
b = "0" + b;
}
for(int i=0 ; i < [Link]() ; i++){
c += ([Link](i) == '0') ? '1' : '0';
}
[Link]("1's complement: " + c);
}else{
[Link]("1's complement: " +
[Link](a));
}
[Link]("2's complement: " + [Link](a));
}
}

OUTPUT
BONUS 2 - Write a program to input a decimal number and find its
hexadecimal value
/*
8/13/2024
Q> Write a program to covert a decimal number to its hexadecimal value
*/
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a decimal number:");


int decimal = [Link]();

String hexadecimal = [Link](decimal).toUpperCase();

[Link]("The hexadecimal equivalent is: " +


hexadecimal);

[Link]();
}
}

OUTPUT

BONUS 3 - Write a program to find the size of all the primitive datatypes
/*
8/13/2024
Q> Write a program to find the size of all the primitive datatypes
*/
public class Main {
public static void main(String[] args) {
[Link]("Size of byte: " + [Link] + " bytes");
[Link]("Size of short: " + [Link] + " bytes");
[Link]("Size of int: " + [Link] + " bytes");
[Link]("Size of long: " + [Link] + " bytes");
[Link]("Size of float: " + [Link] + " bytes");
[Link]("Size of double: " + [Link] + " bytes");
[Link]("Size of char: " + [Link] + " bytes");
[Link]("Size of boolean: " + "JVM dependent");
}
}

OUTPUT

Common questions

Powered by AI

The `Scanner` class in Java facilitates runtime input by providing methods to read different types of input values from various input sources like the standard input stream. In the provided programs, the `Scanner` is used to capture user inputs for first name, last name, and dimensions for geometric calculations, enabling interactive programs that respond to user data .

The area calculation programs for different shapes like circle, square, triangle, and rectangle demonstrate polymorphism by using method overloading within a single class. Each shape uses a different 'area' method signature to handle input parameters unique to that shape. Despite being named similarly, the 'area' methods are polymorphic because they perform differently based on the type of shape being calculated .

Loops, particularly nested loops, are used to efficiently generate multiplication tables by iterating over a range of numbers. In Java, two nested `for` loops are utilized where the outer loop determines the rows and the inner loop calculates and prints the product for each column, thus constructing a structured multiplication table. This approach is both systematic and scalable .

The Java program converts a decimal number to its hexadecimal equivalent using the 'Integer.toHexString()' method, which takes an integer input and returns its hexadecimal representation as a string. The 'toUpperCase()' method is then used to ensure that the resulting string is in uppercase .

The program calculates a number's 1's complement by flipping all bits in its binary representation, essentially converting 0s to 1s and vice versa . The 2's complement is obtained by adding 1 to the 1's complement, which effectively allows for the representation of negative numbers and simplifies arithmetic operations in binary .

The size of primitive datatypes in Java, such as `boolean`, is JVM dependent because the JVM documentation does not specify an exact memory size for `boolean` values. This variability allows JVM implementations the flexibility to choose an efficient way to represent `boolean` values based on underlying hardware and software considerations, which can lead to inconsistencies across different JVMs .

Calculating area at runtime is necessary for the program's interactivity, allowing users to provide different inputs each time the program runs. This is achieved via the `Scanner` class, inviting users to input dimensions, processed through respective methods to compute areas. This enhances flexibility and usability because calculations are tailored to each specific run based on user-provided data .

In handling user input for geometrical calculations, considerations include validating input to ensure that it is numeric and within acceptable ranges (e.g., positive numbers for lengths and radii), managing different data types necessary for calculations, and handling exceptions or incorrect inputs gracefully to prevent runtime errors and provide user feedback or retry options .

The program determines the one's complement for negative numbers by first converting the negative to a positive and then deriving its binary string representation. The bitwise negation is applied to this binary string, effectively flipping all bits to represent the one's complement. For positive numbers, it directly represents the number by showing its binary form, as positive numbers don’t need negation to find their complement .

Heron's formula is implemented by first calculating the semi-perimeter `s` as half of the sum of all triangle sides (`a`, `b`, `c`). The area is then computed using the formula `sqrt(s * (s-a) * (s-b) * (s-c))`, leveraging the `Math.sqrt()` function to calculate the square root, thus finding the area based on side lengths without using the height .

You might also like