KEMBAR78
Java Practice Book Week1 Expanded | PDF | Integer (Computer Science) | Data Type
0% found this document useful (0 votes)
5 views6 pages

Java Practice Book Week1 Expanded

It s Java pdf
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)
5 views6 pages

Java Practice Book Week1 Expanded

It s Java pdf
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
You are on page 1/ 6

■ Java Practice Book – Week 1 (Expanded)

Topic: Variables & Data Types


Variables are used to store data. In Java, every variable must have a type (int, double, char,
boolean, etc.).

■ Types of Variables:
- Local Variable: declared inside a method
- Instance Variable: declared inside class but outside methods
- Static Variable: declared with 'static' keyword

■ Common Data Types:


- int: stores integers (e.g., 10, -20)
- double: stores decimals (e.g., 3.14, 9.81)
- char: stores single character (e.g., 'A', 'z')
- boolean: true or false
- String: sequence of characters (object, not primitive)
■ Practice Questions and Solutions
Q1. Swap two numbers using a temporary variable.
class SwapTemp {
public static void main(String[] args) {
int a=10,b=20,temp;
temp=a; a=b; b=temp;
System.out.println("a="+a+" b="+b);
}
}

Q2. Swap two numbers without using a third variable.


class SwapNoTemp {
public static void main(String[] args) {
int a=5,b=7;
a=a+b; b=a-b; a=a-b;
System.out.println("a="+a+" b="+b);
}
}

Q3. Find the largest of three numbers.


class LargestOfThree {
public static void main(String[] args) {
int a=10,b=25,c=15;
int largest=(a>b)? (a>c?a:c):(b>c?b:c);
System.out.println("Largest="+largest);
}
}

Q4. Check whether a number is even or odd.


class EvenOdd {
public static void main(String[] args) {
int n=7;
if(n%2==0) System.out.println("Even");
else System.out.println("Odd");
}
}

Q5. Calculate area and circumference of a circle.


class Circle {
public static void main(String[] args) {
double r=5;
double area=3.14*r*r;
double circ=2*3.14*r;
System.out.println("Area="+area+" Circumference="+circ);
}
}

Q6. Convert temperature from Celsius to Fahrenheit.


class TempConvert {
public static void main(String[] args) {
double c=37;
double f=(c*9/5)+32;
System.out.println("Fahrenheit="+f);
}
}

Q7. Find simple interest given P, R, T.


class SimpleInterest {
public static void main(String[] args) {
double p=1000,r=5,t=2;
double si=(p*r*t)/100;
System.out.println("Simple Interest="+si);
}
}

Q8. Check whether a year is leap year or not.


class LeapYear {
public static void main(String[] args) {
int year=2024;
if((year%400==0)||(year%4==0 && year%100!=0))
System.out.println("Leap Year");
else
System.out.println("Not Leap Year");
}
}

Q9. Calculate average of five numbers.


class Average {
public static void main(String[] args) {
int a=1,b=2,c=3,d=4,e=5;
double avg=(a+b+c+d+e)/5.0;
System.out.println("Average="+avg);
}
}

Q10. Check whether a number is positive, negative or zero.


class PositiveNegative {
public static void main(String[] args) {
int n=0;
if(n>0) System.out.println("Positive");
else if(n<0) System.out.println("Negative");
else System.out.println("Zero");
}
}

Q11. Convert minutes into hours and minutes.


class MinToHour {
public static void main(String[] args) {
int min=130;
int hr=min/60;
int m=min%60;
System.out.println(hr+" Hours "+m+" Minutes");
}
}

Q12. Calculate perimeter of rectangle given length and width.


class RectanglePerimeter {
public static void main(String[] args) {
int l=10,w=5;
int peri=2*(l+w);
System.out.println("Perimeter="+peri);
}
}

Q13. Convert days into years, weeks and days.


class DaysConvert {
public static void main(String[] args) {
int days=370;
int years=days/365;
days%=365;
int weeks=days/7;
int d=days%7;
System.out.println(years+" Years "+weeks+" Weeks "+d+" Days");
}
}

Q14. Find ASCII value of a character.


class AsciiValue {
public static void main(String[] args) {
char c='A';
int val=c;
System.out.println("ASCII="+val);
}
}

Q15. Check whether a character is vowel or consonant.


class VowelConsonant {
public static void main(String[] args) {
char c='e';
if("aeiouAEIOU".indexOf(c)!=-1)
System.out.println("Vowel");
else
System.out.println("Consonant");
}
}

Q16. Find square and cube of a number.


class SquareCube {
public static void main(String[] args) {
int n=4;
System.out.println("Square="+(n*n)+" Cube="+(n*n*n));
}
}

Q17. Find sum of digits of a number.


class DigitSum {
public static void main(String[] args) {
int n=123, sum=0;
while(n!=0){ sum+=n%10; n/=10; }
System.out.println("Sum="+sum);
}
}

Q18. Reverse a 3-digit number.


class ReverseNum {
public static void main(String[] args) {
int n=123, rev=0;
while(n!=0){ rev=rev*10+n%10; n/=10; }
System.out.println("Reverse="+rev);
}
}

Q19. Convert given seconds into hours, minutes and seconds.


class SecConvert {
public static void main(String[] args) {
int sec=3670;
int hr=sec/3600;
sec%=3600;
int min=sec/60;
int s=sec%60;
System.out.println(hr+":"+min+":"+s);
}
}

Q20. Calculate compound interest given P, R, T.


class CompoundInterest {
public static void main(String[] args) {
double p=1000,r=5,t=2;
double ci=p*Math.pow((1+r/100),t)-p;
System.out.println("Compound Interest="+ci);
}
}
■ Mini Project: Unit Converter
Description: Create a program that converts between units (km to miles, Celsius to Fahrenheit, etc.)
import java.util.Scanner;
class UnitConverter {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("1.Km to Miles 2.Celsius to Fahrenheit 3.Minutes to Hours");
int choice=sc.nextInt();
switch(choice){
case 1: System.out.print("Enter Km: ");
double km=sc.nextDouble();
System.out.println("Miles="+(km*0.621371));
break;
case 2: System.out.print("Enter Celsius: ");
double c=sc.nextDouble();
System.out.println("Fahrenheit="+((c*9/5)+32));
break;
case 3: System.out.print("Enter Minutes: ");
int m=sc.nextInt();
System.out.println("Hours="+(m/60)+" Min="+(m%60));
break;
default: System.out.println("Invalid choice");
}
}
}

You might also like