Solved Assignment Problems in Java – Part2

Q1. Design a program to find the circumference of a circle. Use the formula: C=2πr, where π is approximately equivalent 3.1416.

Sol:

package circumference;
import java.util.Scanner;
public class circumference{
public static void main(String [] args) {
Scanner Ob1 = new Scanner(System.in);
System.out.println(“Enter radius of circle r: “);
int r = Ob1.nextInt();
double pi = 3.1416;
double Circum = 2pir;
System.out.println(“circumference of circle is:” +Circum);
}
}

Q2. Write a program that takes as input the purchase price of an item (P), its expected number of years of service (Y) and its expected salvage value (S). Then outputs the yearly depreciation for the item (D). Use the formula: D = (P – S) Y.

Sol:

package depreciation;
import java.util.Scanner;
public class depreciation{
public static void main(String [] args) {

Scanner Ob1 = new Scanner(System.in);
System.out.println(“Enter purchase price of an item P: “);
float P = Ob1.nextInt();

System.out.println(“Enter expected salvage value S: “);
float S = Ob1.nextInt();

System.out.println(“Enter expected number of years of service Y”);
float Y = Ob1.nextInt();

double D = (P-S)*Y;
System.out.println(“Product depreciation is:” +D);
}
}

Q3. Swapping of 2 variables without using temporary (or 3rd variable).

Sol:

package swap_variables;
import java.util.Scanner;
public class swap_variables{
public static void main(String [] args) {

Scanner Ob1 = new Scanner(System.in);
System.out.println(“Enter value of x: “);
int x = Ob1.nextInt();

System.out.println(“Enter value of y: “);
int y = Ob1.nextInt();

System.out.println(“Before swapping, values of x and y are: ” +x +”\t” +y);
x = x+y; //suppose x = 10, y = 20, then x = 10+20 = 30
y = x-y; //y = x-y = 30 – 20 = 10
x = x-y; //x = x – y = 30 – 10 = 20

System.out.println(“After swapping, values of x and y are: ” +x +”\t” +y);
}
}

Q4. Write a program to compute the radius of a circle. Derive your formula from the given equation: A=πr², then display the output.

Sol:

import java.util.Scanner;
import java.lang.Math;
public class CircleRadius{
public static void main(String [] args) {

Scanner Ob1 = new Scanner(System.in);
System.out.println(“Enter area of circle A: “);
int A = Ob1.nextInt();

double pi = 3.1416;
double r = Math.sqrt(A/pi);

ystem.out.println(“Radius of circle is:” +r);
}
}

Q5. Determine the most economical quantity to be stocked for each product that a manufacturing company has in its inventory: This quantity, called economic order quantity (EOQ) is calculated as follows: EOQ=2rs/1 where: R= total yearly production requirement S=set up cost per order I=inventory carrying cost per unit.

Sol:

import java.util.Scanner;
public class EOQ{
public static void main(String [] args) {

Scanner Ob1 = new Scanner(System.in);
System.out.println(“Enter total yearly production R:”);
int R = Ob1.nextInt();

System.out.println(“Enter set up cost S:”);
int S = Ob1.nextInt();

System.out.println(“Enter inventory cost I:”);
int I = Ob1.nextInt();

double EOQ = (2RS)/I;
System.out.println(“economic order quantity EOQ is:” +EOQ);
}
}

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »