[Go to site: main page, start]

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

Java Programs for Class VIII Assignments

The document contains a series of Java programming assignments for Class VIII, including tasks such as printing messages, calculating sums, areas, and electricity bills, as well as handling user inputs and performing basic arithmetic operations. Each assignment includes a brief description followed by the corresponding Java code. The document also addresses error correction in provided code snippets.

Uploaded by

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

Java Programs for Class VIII Assignments

The document contains a series of Java programming assignments for Class VIII, including tasks such as printing messages, calculating sums, areas, and electricity bills, as well as handling user inputs and performing basic arithmetic operations. Each assignment includes a brief description followed by the corresponding Java code. The document also addresses error correction in provided code snippets.

Uploaded by

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

Class VIII (Java Programs)

Assignment I

Q1 WAP to print a message “Hello world”.

// Program to print a message


public class first
{
public static void main()
{
[Link]("Hello World");

}//main closed
}//class closed

Q2 WAP to print your name along with details in different lines

// Program to print your name and details in different lines


public class second//class declared
{
public static void main()
{
[Link]("My name is Rohit");
[Link]("I study class in 8th");
[Link]("In City Montessori School");
[Link]("My DOB is 12th August 2012");

} //main closed
} //class closed

Q3 WAP to print the sum of two numbers where first number is 15 and second number is 56

// Program to print the sum of two numbers


public class third
{
public static void main()
{
int a=15; //variable declaration
int b=56;
int sum = a+b;
[Link]("Sum= " +sum);

} //main closed
} //class closed

Q4 WAP to calculate the perimeter of a rectangular field whose length is 234 metre and breadth is 120
metre
//Program to calculate the perimeter of a rectangular field
class perimeter
{
public static void main()
{
int l= 234;
int b= 120;
int per= 2 * ( l + b);
[Link]("the perimeter ="+ per +"m");
} //main closed
} //class closed

Q5 WAP to calculate the area of a square whose


side is 15 cm area= side * side

// Program to calculate the area of a square whose

public class area


{
public static void main()
{
int s=15;
int a= s * s;
[Link]("Area of square ="+a);
} //main closed
} //class closed

Q6 WAP to calculate the cost of 20 pens when the cost of one pen is Rs 15
// Program to calculate the cost of 20 pens
public class cost
{
public static void main()
{
int pen_price=15;
int total_pens=20;
int amount =pen_price * total_pen;
[Link]("Amount to purchase 20 pens ="+amount);
} //main closed
} //class closed

Q7 WAP to calculate the electricity bill when the unit consumed are 450 and price of per unit is Rs
4.80 paisa also add Rs500 meter charge
// Program to calculate electricity bill
public class bill
{
public static void main( )
{
int units=450;
int meter=500;
double price=4.80;
double total= (units * price)+meter;
[Link]("Electricity bill="+ total);
} //main closed
} // class closed

Q8 Mayank lent a sum of Rs 5000 for 5 years at a rate of 15% per annum to his friend Ajeet. Akash
pays back the total amount along with the [Link] in java to calculate the total amount to be
paid back along with the interest.

// Program to calculate simple interest


public class simple_interest
{
public static void main( )
{
double principle=5000.0;
double rate=15.0;
int time= 5;
double interest=(principle * time * rate)/100.0;
double amount = principle + interest;
[Link]("Ajeet paid back " +amount + "Rs to Manoj");
}//main closed
}// class closed
[Link] a program in Java to accept marks as arguments in the main method and calculate total
and average marks for three subjects English Maths and [Link] program should also take
your name as input and should print your total marks and your name and your average.(Print the
name of student on one line and total marks and average on the next line.)

public class marks


{
public static void main(String nm,int s1,int s2,int s3)
{
int tot = s1+s2+s3;
int avg = (tot/3);
[Link](" Name=" + nm);
[Link](" Total marks=" + tot);
[Link](" Average=" + avg);
}
}

Q10. WAP in Java to interchange the values of variables (using third variable and without using
third variable)
using third variable—->
public class swap
{
public static void main()
{
int a = 5;
int b = 6;
[Link]("The old value of a= " + a);
[Link]("The old value of b= " + b);
int c = b;
b = a;
a = c;
[Link]("The new value of a= " + a);
[Link]("The new value of b= " + b);
}
}

Without using third variable—->


public class swap
{
public static void main(String[] args)
{
int a = 5; // First variable
int b = 10; // Second variable

[Link]("Before swapping:");
[Link]("a = " + a + ", b = " + b);

// Swapping without third variable


a = a + b; // a becomes 15
b = a - b; // b becomes 5
a = a - b; // a becomes 10
[Link]("After swapping:");
[Link]("a = " + a + ", b = " + b);
}
}

[Link] IN JAVA TO ENTER TWO NUMBERS AND FIND THEIR SUM DIFFERENCE PRODUCT AND
DIVISION
class calculator
{
public static void main(int a,int b)
{
int sum = a + b;
int difference = a - b;
int product = a * b;
int division=a/b;
[Link]("Sum = " + sum);
[Link]("Difference = " + difference);
[Link]("Product = " + product);
[Link]("Division = " + division);
}
}

[Link] to input a number from the user and print its square
class square
{
public static void main (int num)
{
int sq = num * num;
[Link]("Square of " + num + " is: " + sq);
}
}

[Link] to input two digit [Link] and print the digit at units and tens place.
//program to separate and print the digit at units and tens place.
class number
{
public static void main (int num)
{
int tens = num / 10; // Tens place
int units = num % 10; // Units place
[Link]("Tens place: " + tens);
[Link]("Units place: " + units);
}
}

[Link] to input time in hours and convert into minutes and seconds and print it
//program to input time in hours and convert into minutes and seconds and print it
class time
{
public static void main(int hours)
{
int minutes = hours * 60;
int seconds = hours * 3600;
[Link](hours + " hours = " + minutes + " minutes");
[Link](hours + " hours = " + seconds + " seconds");
}
}

[Link] to input distance travelled and time taken by a [Link] the speed of the train

class train
{
public static void main (double dis,double time)
{
double speed=dis/time;
[Link]. println(“speed of train=”+speed);
}
}

16. WAP to input marks of 5 [Link] the sum and percentage of the student.
// program to print sum and percentage of the student
class student
{
public static void main(int sub1,int sub2,int sub3,int sub4,int sub5)
int total = sub1 + sub2 + sub3 + sub4 + sub5;
double percentage = (total / 5.0);
[Link]("Total Marks = " + total);
[Link]("Percentage = " + percentage + "%");
}
}

17. WAP to input radius and calculate the area and circumference of a circle.
class circle
{
public static void main(int r)
{
double area=3.14*r*r;
double circum=2*3.14*r;
[Link]("area of circle= " +area);
[Link]("circumference of circle= " +circum);
}
}
Q18. Rewrite the following code after removing errors in it

class Prg()
{
void main
{
int x,y;
x=5
Y = 6;
[Link](x + y);
}
}

The corrected code

class Prg
{
public static void main()
{
int x, int y;
x = 5;
y = 6;
[Link](x + "+ " + y);
}
}

[Link] errors in the given code and write the correct code
//program to accept two integers and print their sum

Class abc
{
Void main (int a,b)
{
int x= a + b;
[Link]("sum=x");
}
}

The corrected code:-

class abc
{
public static void main (int a, int b)
{
int x= a + b;
[Link]("sum=" +x);
}
}

Common questions

Powered by AI

Swapping without a third variable is achieved using arithmetic operations: the sum of both variables is stored in one of them, the second variable is then updated as this sum minus its initial value (which represents the initial value of the first), and finally, the first variable is updated as this new value minus its current state (which represents the initial value of the second). This eliminates the need for a placeholder while maintaining variable integrity .

The program calculates the average by summing the marks of three subjects and dividing the total by three. It assumes that the inputs are valid integers representing the marks. The document provides a function that takes marks and a name as arguments, then computes and prints the total and average, assuming uniform weight for each subject's marks .

The approach calculates the total electricity bill by multiplying the number of units by the price per unit and adding a fixed meter charge. A strength of this method is its straightforwardness and clarity in calculating additional costs. A potential weakness is its assumption of a constant price per unit without considering varying energy tiers or discounts, which might exist in practical billing scenarios .

The calculation of the total amount paid back involves determining the simple interest using the formula: interest = (principal * rate * time) / 100. The total amount is then computed by adding the calculated interest to the principal. Key elements involved include the principal amount of Rs 5000, the interest rate of 15% per annum, and the time period of 5 years, yielding a total repayment amount .

The document demonstrates the calculation of a rectangle's perimeter using the formula: perimeter = 2 * (length + breadth). Variables are declared to store the length and breadth (234 meters and 120 meters, respectively), which are then used in the formula to compute the perimeter, showing practical application in coding .

The correct approach sets the class name with proper naming conventions and specifies the main method using 'public static void main()'. Corrections included proper syntax for declaring and initializing variables, correcting statement capitalization such as converting 'Void' to 'void', and properly formatting the System.out.println() statement .

The document describes a method for interchanging values using a temporary variable where you first assign one of the two variables to the temporary variable, then assign the value of the second variable to the first, and finally assign the value of the temporary variable to the second. This method relies on storing one value temporarily to prevent data loss during the swap .

The technique used sets speed as distance divided by time, speed = distance / time. This applies the common formula from physics to compute how quickly an object covers a distance in a given time period, effectively using it in a program to process numerical inputs and outputs .

The program calculates the area of a circle using the formula area = π * r^2, and the circumference using circumference = 2 * π * r, where r is the radius. These use fundamental principles of circle geometry indicating the relation between radius, area, and circumference using π (approximately 3.14).

The program calculates simple interest by implementing the formula interest = (principal * rate * time) / 100, followed by computing the total amount by adding the principal to the interest. This illustrates good coding practices such as using descriptive variable names, structured calculations, and integration of arithmetic methods in Java to achieve real-world problem-solving .

You might also like