You are on page 1of 18

Assignment 1 Object Oriented Programming Using Java

For this assignment, you will solve problems based on what you have learned
in topics of Java programming viz. Elementary Programming, Selections,
Loops
Instructions
Review notes of the Chapter.
There are 21 questions in this assignment.
Assignment submitted after due date will not be evaluated and a score of zero
will be awarded for this assignment.
Upload a pdf version of the document.
Due Date: 5 pm, August 31, 2015.
Submitting this Assignment
You will submit (upload) this assignment in Blackboard. Email/paper submissions will not
be accepted.

Write code for the program after each question in this document followed by the screen
print of output.

Questions must be answered in the given order.

Name this document as A1_2015_John_Doe.pdf in case your name is John Doe.

Grading Criteria
Correct and to-the-point answers will be awarded full points. This assignment has 5
points (with weightage of 5% in your overall 100 points).
Questions:
1. Write a program that reads three edges for a triangle and determines whether the input is
valid. The input is valid if the sum of any two edges is greater than the third edge.
CODE:
import java.util.Scanner;
public class Assignment_1 {
public static void main(String[] args){
float side1,side2,side3;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Length of side 1");
side1=sc.nextFloat();
System.out.println("Enter Length of side 2");
side2=sc.nextFloat();
System.out.println("Enter Length of side 3");
side3=sc.nextFloat();

Assignment 1 Object Oriented Programming Using Java


if(side1+side2<side3)
System.out.println("Wrong Inputs");
else if(side2+side3<side1)
System.out.println("Wrong Inputs");
else if(side3+side1<side2)
System.out.println("Wrong Inputs");
else
System.out.println("Correct Inputs");
}
}

OUTPUT:

2. Write a program to print ASCII value of all characters.


CODE:
public class Assignment_1{
public static void main(String[] args){
for(int i=0;i<127;i++)
System.out.print((char)i+" - "+i+"\n");
}
}

OUTPUT:

Assignment 1 Object Oriented Programming Using Java


3. Write a program to find out sum of digits of a given number.
CODE:
import java.util.Scanner; public
class Assignment_1{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int
num,sum=0;
System.out.println("Enter your number");
num=sc.nextInt();
while(num!=0){
sum=sum+(num%10);
num=num/10;
}
System.out.println(sum);
}
}

OUTPUT:

4. Write a program to find out the L.C.M. and H.C.F. of two numbers.
CODE:
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args){
int num1,num2,hcf,lcm,prod;
hcf=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number 1");
num1=sc.nextInt();
System.out.println("Enter number 2");
num2=sc.nextInt();
prod=num1*num2;
for(int i=2;i<prod;++i){
if(num1%i==0&& num2%i==0)
hcf=i;
}
lcm=prod/hcf;
System.out.println("HCF is "+hcf+" LCM is "+lcm);
}
}

OUTPUT:

Assignment 1 Object Oriented Programming Using Java


5. Write a program that prompts the user to enter the center coordinates and radii of two circles
and determines whether the second circle is inside the first or overlaps with the first.
CODE:
import java.util.Scanner;
import java.lang.Math;
public class Assignment_1{
public static void main(String[] args){
double x1,y1,x2,y2,r1,r2,dist;
Scanner
sc=new Scanner(System.in);
System.out.println("Enter the x coordinate of circle 1");
x1=sc.nextDouble();
System.out.println("Enter the y coordinate of circle 1");
y1=sc.nextDouble();
System.out.println("Enter the x coordinate of circle 2");
x2=sc.nextDouble();
System.out.println("Enter the x coordinate of circle 2");
y2=sc.nextDouble();
System.out.println("Enter the radius of circle 1");
r1=sc.nextDouble();
System.out.println("Enter the radius of circle 2");
r2=sc.nextDouble();
dist=Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
System.out.println(dist);
if(dist>r1+r2)
System.out.println("The Circles do not overlap");
else if(dist<r1+r2&&dist>Math.abs(r1-r2))
System.out.println("The circles intersect");
else
if(dist<Math.abs(r1-r2))
System.out.println("The circles overlap");
}
}

OUTPUT:

6. Write a program that prompts the user to enter a decimal integer and displays its
corresponding binary value. Dont use Javas Integer.toBinaryString(int) in this program.

Assignment 1 Object Oriented Programming Using Java


CODE:
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int number;
int arr[]=new int[25];
System.out.println("Enter decimal you want to convert into binary");
number=sc.nextInt();
int index = 0;
while(number > 0){
arr[index++] = number%2;
number = number/2;
}
System.out.println("The decimal equivalent is :");
for(int i = index-1;i >= 0;i--){
System.out.print( arr[i]);
}
}
}

OUTPUT:

7. Write a program that prompts the user to enter a decimal integer and displays its
corresponding hexadecimal value. Dont use Javas Integer.toHexString(int) in this
program.
CODE:
import java.util.Scanner;
class Assignment_1
{
public static void main(String args[])
{
Scanner sc = new Scanner( System.in );
System.out.print("Enter a decimal number : ");
int num =sc.nextInt();
int rem;
String str2="";
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(num>0)
{
rem=num%16;
str2=hex[rem]+str2;
num=num/16;
}
System.out.println("Decimal to hexadecimal: "+str2);
}
}

Assignment 1 Object Oriented Programming Using Java


OUTPUT:

8. Write a program that simulates flipping a coin one million times and displays the number of
heads and tails.
CODE:
import java.util.Random;
public class Assignment_1{
public static void main(String[] args) {
int head=0,tail=0,r;
Random rand=new Random();
for(int i=0;i<1000000;++i){
r=(int)(Math.random()*2);
if(r==1)
head++;
else tail++;
}
System.out.println("No of heads "+head+" No of tails "+tail);
}
}

OUTPUT:

9. Write a program that reads integers, finds the largest of them, and counts its occurrences.
Assume that the input ends with number 0. Suppose that you entered 3 5 2 5 5 5 0; the
program finds that the largest is 5 and the occurrence count for 5 is 4.
CODE:
import java.util.Scanner;
public class Assignment_1 {
public static void main (String [] args){
System.out.println("Enter integers(0 to exit)");
Scanner in = new Scanner(System.in);
int max = -1;
int count = 0;
int number;
while((number = in.nextInt()) != 0){
if(number > max){
max = number;
count = 1;
} else if (number == max){
count++;
}
}
System.out.println("Max number is :"+max);
System.out.println("Count is :"+count);
}
}

Assignment 1 Object Oriented Programming Using Java


OUTPUT:

10. Write a program that prompts the user to enter the number of seconds, displays a message at
every second, and terminates when the time expires.
CODE:
import java.util.Scanner; class
Assignement_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of seconds");
int sec = sc.nextInt();
while (sec > 0) {
System.out.println(sec +" sec remaining");
long start = System.currentTimeMillis();
while
(start + 1000 > System.currentTimeMillis());
sec-;
}
}
}

OUTPUT:

11. A solution to find the greatest common divisor of two integers n1 and n2 is as follows: First
find d to be the minimum of n1 and n2, then check whether d, d-1, d-2, 2, or 1 is a divisor for
both n1 and n2 in this order. The first such common divisor is the greatest common divisor
for n1 and n2. Write a program that prompts the user to enter two positive integers and
displays the gcd.

Assignment 1 Object Oriented Programming Using Java


CODE:
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n1,n2,d,gcd=1;
System.out.println("Enter Number 1");
n1=sc.nextInt();
System.out.println("Enter Number 2");
n2=sc.nextInt();
if(n1>n2)
d=n2;
else
d=n1;
while (d!=1) {
if (n1 % d == 0 && n2 % d == 0) {
gcd = d;
break;
}
d--;
}
System.out.println("gcd is " + gcd);
}
}

OUTPUT:

Assignment 1 Object Oriented Programming Using Java

12. Write a program to input a set of integers and count the number of primes.
CODE:
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args) {
int n,count,flag;
count=0;
flag=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of integers you want to check");
n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the numbers");
for(int i=0;i<n;++i){
arr[i]=sc.nextInt();
}
for(int i=0;i<n;++i){
for(int j=2;j<=arr[i]/2;++j){
if(arr[i]%j==0) {
flag = 0;
break;
}
else flag=1;
}
if(flag==1)
count++;
}
System.out.println("Number of primes are "+count);
}
}

OUTPUT:

Assignment 1 Object Oriented Programming Using Java

13. Write a program to determine input the marks of n students in a subject and determine the
frequency count of marks obtained i.e. how many students obtained 100, how many 99, how
many 98 and so on up to 0.
CODE:
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args) {
int n,x;
int arr[]=new int[101];
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of students whose marks you want to enter
");
n=sc.nextInt();
for(int i=1;i<=n;++i){
System.out.println("Enter marks of student "+ i);
x=sc.nextInt();
arr[x]++;
}
for(int i=0;i<101;++i){
if(arr[i]!=0)
System.out.println("Number of students with marks "+i +" are
"+arr[i]);
}
}
}

OUTPUT:

Assignment 1 Object Oriented Programming Using Java

14. Write programs to produce each of the following patterns as output:


(a)
*****
*****
*****
*****
*****
(b)
(c)
*
*****
**
****
***
***
****
**
*****
*
CODE:
public class Assignment_1{
public static void main(String[] args) {
for(int i=0;i<5;++i){
for(int
j=0;j<5;++j){
System.out.print("*");
}
System.out.println(" ");
}
System.out.println(" ");
for(int i=1;i<7;++i){
for(int j=1;j<i;++j){
System.out.print("*");
}
System.out.println(" ");
}
System.out.println(" ");
for(int i=1;i<6;++i){
for(int j=6-i;j>0;--j){
System.out.print(" * ");
}
System.out.println(" ");
for(int x=0;x<i;++x)
System.out.print(" ");
}
}
}

Assignment 1 Object Oriented Programming Using Java

OUTPUT:

15. Write a program to read an integer and reverse it. Your program output would look like this:
12345 (input)
54321 (output)
(Hint: Use a combination of % and / operations to do this).
CODE:
import java.util.Scanner; public
class Assignment_1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num,revnum;
revnum=0;
System.out.println("Enter the number you want to reverse");
num=sc.nextInt();
while(num!=0){
revnum=revnum*10+num%10;
num=num/10;
}
System.out.println("The num after reversing is "+ revnum);
}
}

Assignment 1 Object Oriented Programming Using Java


OUTPUT:

16. Write an interactive program that will convert a positive integer quantity to a roman numeral
(e.g., 12 will be converted to XII, 14 will be converted to XIV, and so on). Design the
program so that it will execute repeatedly, until a value of zero is read in from the keyboard.
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num=1;
while(num!=0) {
System.out.println("Enter number to be converted into roman numerals");
num = sc.nextInt();
if (num > 0 && num < 4000) {
String thou[] = {"", "M", "MM", "MMM"};
String hund[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC",
"DCCC", "CM"};
String ten[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX",
"LXXX", "XC"};
String unit[] = {"", "I", "II", "III", "IV", "V", "VI", "VII",
"VIII", "IX"};
int
int
int
int

th = num / 1000;
h = (num / 100) % 10;
t = (num / 10) % 10;
u = num % 10;

System.out.println("Roman Equivalent= " + thou[th] + hund[h] +


ten[t] + unit[u]);
} else
System.out.println("You entered a number out of Range.Please enter
a number in the range [1-3999]");
}
}
}

OUTPUT:

Assignment 1 Object Oriented Programming Using Java


17. An Armstrong number is one in which the sum of the cubes of digits of a number is equal to
the original number. Write a program to check given number is Armstrong number or not.
For example: n=153 => 13 + 53 +33 = 1+125+27= 153, so 153 is an Armstrong number.
CODE:
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args) {
int n,x,temp,f,sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
x=n;
sum=0;
while(x!=0){
f=x%10;
temp=1;
for(int i=0;i<3;++i){
temp=temp*f;
}
sum=sum+temp;
x=x/10;
}
if(sum==n)
System.out.println("The number is a Armstrong Number");
else
System.out.println("The number is not Armstrong");
}
}

OUTPUT:

18. Write a program to generate the following pyramid of digits, using nested loops. (Hint: Try
to develop a general expression to print out the appropriate line)
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210

Assignment 1 Object Oriented Programming Using Java


CODE:
class assignment_1 {
public static void main(String[] args) {
int x = 10;
for (int i = 1; i <= x; i++) {
for (int j = x - i; j >= 0; j--) {
System.out.print(" ");
}
int r = ((i * 2) - 1);
int c = i - 1;
for (int j = 1; j <= r; j++) {
if (j <= (r / 2 + 1)) {
c++;
} else
c--;
if (c > 9) {
c = 0;
}
if (c < 0) {
c = 9;
}
System.out.print(c);
}
System.out.println();
}
}
}

OUTPUT:

Assignment 1 Object Oriented Programming Using Java


19. In a strong number, the sum of the factorials of digits of a number is equal to the original
number. Write a program to check given number is strong number or not.
CODE:
import java.util.Scanner;
public class Assignment_1{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n,temp,i,f,sum,x;
sum=0;
System.out.println("Enter number");
n=sc.nextInt();
x=n;
while(x!=0){
i=1;
f=1;
temp=x%10;
while(i<=temp){
f=f*i;
i++;
}
sum=sum+f;
x=x/10;
}
if(n==sum)
System.out.println("Number is strong");
else
System.out.println("Number is not strong");
}
}

OUTPUT:

20. Write a program to generate multiplication tables for 1 ,2, , 10. Each table up to 10 should
look as follows:
2x1=2
2x2=4
...
2x10=20

Assignment 1 Object Oriented Programming Using Java


CODE:
public class Assignment_1{
public static void main(String[] args) {
for(int i=1;i<=10;++i){
System.out.println(" ");
for(int
j=1;j<=10;++j){
System.out.println(i+" X " + j +" = " +i*j );
}
}
}
}

OUTPUT:

21. Modify (20) so that your output now looks like this:
2x1=2 3x1=3 ... 5x1=5 2x2=4
3x2=6 ... 5x2=10
...
...
2x10=20 3x10=30 ... 5x10=50
CODE:
public class Assignment_1{
public static void main(String[] args) {
for(int i=1;i<=10;++i){
System.out.println(" ");
for(int j=1;j<=10;++j){
System.out.println(i+" X " + j +" = " +i*j );
}
}
}
}

Assignment 1 Object Oriented Programming Using Java


OUTPUT: