A1 2015

Embed Size (px)

DESCRIPTION

assignment

Citation preview

  • 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

  • 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

  • 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(distMath.abs(r1-r2))

    System.out.println("The circles intersect"); else

    if(dist

  • 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 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

  • 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

  • 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

  • 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 th = num / 1000;

    int h = (num / 100) % 10;

    int t = (num / 10) % 10;

    int 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

  • 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 = 0; j--) {

    System.out.print(" ");

    }

    int r = ((i * 2) - 1);

    int c = i - 1;

    for (int j = 1; j

  • 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

  • Assignment 1 Object Oriented Programming Using Java

    CODE:

    public class Assignment_1{ public static void main(String[] args) { for(int i=1;i

  • Assignment 1 Object Oriented Programming Using Java

    OUTPUT: