introduction to software...

47
Introduction to Software Development (CE00371-1) Page | 1 INTRODUCTION TO SOFTWARE DEVELOPMENT Individual Assignment “TUTORIAL 8,9,10” Date Due: November, 28, 2014 Submitted to: Submitted by: PT1381110 Asia Pacific Institute of Information Technology level 1

Upload: others

Post on 29-Oct-2019

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 1

INTRODUCTION TO SOFTWARE DEVELOPMENT

Individual Assignment

“TUTORIAL 8,9,10”

Date Due: November, 28, 2014

Submitted to: Submitted by:

Gaurav Gambhir Krishnakant Kumar

Module Lecturer (PT1381110)

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 2: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 2

TABLE OF CONTENTS

ContentsACKNOWLEDGEMENT.........................................................................................................3Certificate...................................................................................................................................4

TUTORIAL 8.........................................................................................................................5TUTORIAL 9.......................................................................................................................13Output :-...............................................................................................................................22Output :-...............................................................................................................................24Output :-...............................................................................................................................25TUTORIAL 10.....................................................................................................................27

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 3: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 3

ACKNOWLEDGEMENT

I owe the deep depth of gratitude to our esteemed lecturer Mr. Gaurav Gambhir for

supporting me a lot to complete my assignment and to help me when I had faced several

problems.

I am thankful to the lab staffs for providing necessary help whenever it was required.

Also I like to thank my friends for their help in editing my work and let me know about my

errors and mistakes throughout the span.

Krishnakant Kumar

(PT1381110)

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 4: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 4

Certificate

This is to certify that Krishnakant

Kumar Inkate No. PT1381110 has

completed the Individual

assignment of the ISD within time.

I put my all effort to complete my assignment. Also I have designed all the requirements

which it need and submitted the assignment within the due date according to the Staffordshire

University.

Signature:

Gaurav Gambhir

Module Lecturer

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 5: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 5

TUTORIAL 8

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 6: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 6

1. Write a program to assign 10 different numbers in a single dimensional array (S.D.A) and display the greatest number of the elements.

Program:-

import java.util.Scanner;class greatest{

public static void main (String []args){

int high=0; int number[]=new int[10];

Scanner s=new Scanner(System.in);System.out.println("Enter ten numbers:");for(int i=0; i<10; i++){

number[i]=s.nextInt();}for(int i=0;i<10;i++){

if( high<number[i]){

high=number[i];}

}System.out.println("\nThe greatest number is "+high );

} }

Output:-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 7: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 7

2. Write a program to accept 10 states and 10 capitals of a country in two different single dimensional array, now enter the state of a country to display the capital and if it is not present then display the relevant message.

Program:-

import java.util.Scanner;public class capital{

public static void main(String []args){

int i;String state[] = new String[10];String capital[] = new String[10];Scanner s = new Scanner(System.in);System.out.println("Enter any 10 states of India:");for(i=0; i<10; i++){

System.out.println("Enter the state "+(i+1)+" :");

state[i] = s.next();}System.out.println("\nEnter the respective capitals of the entered states:");for(i=0; i<10; i++){

System.out.println("Enter the capital of state "+(i+1)+" :");capital[i] = s.next();

}System.out.println("\nEnter a state to find its Capital:");String sta = s.next();for(i=0; i<10; i++){

if(state[i].equalsIgnoreCase(sta)){

System.out.println("\nCapital of the "+state[i]+" is "+capital[i]);

break;}else{

System.out.println("\nThe state does not match with the available capitals");

break;}

}}

}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 8: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 8

Output:-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 9: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 9

3. Write a program to store day, maximum temperature, minimum temperature of the week in a double dimensional array of 7*3, the program accepts a particular day of the week and display a maximum and minimum temperature of that very day. Use Input stream statement for the same.

Program:- import java.util.Scanner;class temperature{public static void main(String []args){

String day[][]=new String[8][3];int min_temp[][]=new int[8][3];int max_temp[][]=new int[8][3];int d;Scanner s = new Scanner(System.in);System.out.println("Enter the temperature details of day:");for(int i=1;i<=7;i++){

System.out.println("Enetr day "+(i)+" of the week:");day[i][0]=s.next();System.out.println("Enter minimum temperature of the day:");min_temp[i][1]=s.nextInt();System.out.println("Enter maximum temperature of the day:");max_temp[i][2]=s.nextInt();

}System.out.println("Enter the day number to know the temperature range");d=s.nextInt();do{

if(d>0&&d<8){System.out.println("The day is "+day[d][0]);System.out.println("Maximum temperature of the day is "+max_temp[d][2]);System.out.println("Minimum temperature of the day is "+min_temp[d][1]);}elseSystem.out.print("Invalid choice");

}while(d<1||d>7); }}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 10: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 10

Output:-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 11: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 11

4. A metropolitan hotel has 10 floors numbered from 0 to 9, each floor having 50 rooms. Using a single scripted variable for the name of the customers and a double scripted variable R(F,I) where F and I represent floor and room number respectively, write a program for room allocation work. The program should automatically record the name of the customer and the room number.

Program:-

import java.util.Scanner;class Hotel{

public static void main(String args[]){

Scanner s= new Scanner(System.in);int k=0, F, I=0;String name[]= new String[500];String R[][] =new String[10][50];for(F=0 ; F<10 ; F++){

for(I=0 ; I<50 ; I++){System.out.println("\nEnter the name of the visitor:");name[k] = s.next();System.out.println("Allocating your room please wait...");R[F][I] = name[k];System.out.println(name[k]+ ", you are allocated room" + (I+1) + " on

floor" + (F+1));k=k+1;continue;}

}}

}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 12: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 12

Output :-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 13: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 13

TUTORIAL 9

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 14: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 14

1. Write the code to do the following

(a) create an instance of a vector

program:-

import java.util.Vector;class Instance {

public static void main(String[] args) {

Vector v=new Vector();System.out.println(v.capacity());

}}

Output :-

(b) insert a new element into a vector

Program:-

import java.util.Vector;class qb{

public static void main(String[] args) {

int array[]=new int[5];Vector v1=new Vector();v1.addElement(1);v1.addElement(2);System.out.println(v1);v1.insertElementAt(3,2);System.out.println(v1);

}}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 15: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 15

Output:-

(b) remove an element from a vector

Program:-

import java.util.Vector;class Remove {

public static void main(String[] args) {

Vector v=new Vector();v.addElement(11);v.addElement(12);v.addElement(13);v.addElement(14);v.addElement(15);System.out.println(v);v.removeElementAt(3);System.out.println(v);

}}

Output:-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 16: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 16

(d) find the size of a vectorProgram:-

import java.util.Vector;class Size{

public static void main(String[] args) {

Vector size=new Vector();size.addElement(1);size.addElement(2);size.addElement(3);size.addElement(4);size.addElement(5);System.out.println(size.size());

}}

Output :-

2. Explain why inserting additional elements into a Vector object whose current size is less than its capacity is a relatively fast operation and why inserting additional elements into a Vector object whose current size is at capacity is a relatively slow operation.

Vector object whose current size is at capacity is a relatively slow operation.

A vector whose current size is less than its capacity has memory available.

Insertions

are fast because new memory does not need to be allocated. A vector that is at

its capacity

must have its memory reallocated and the existing values copied into it.

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 17: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 17

3. Use a Vector to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000, or a total of $650. Write an application (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson's salary is truncated to an integer amount):

$200-$299$300-$399$400-$499$500-$599$600-$699$700-$799$800-$899$900-$999$1000 and over

Summarize the results in tabular format.

Program:-

import java.util.*;

import java.io.*;

class company

{

public static void main(String args[])

{

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 18: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 18

int count[]=new int[9];

int totalSalary=0, basicSalary=200;

Scanner s=new Scanner(System.in);

Vector v=new Vector();

char c;

do

{

System.out.println("Enter the sales of Salesperson ");

System.out.println(" ===============================================");

int sale=s.nextInt();

totalSalary=(int)(basicSalary+(sale*0.09));

v.addElement(totalSalary);

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 19: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 19

System.out.println("Do you want to continue? (y/n)");

System.out.println(" ===============================================");

String ch=s.next();

c = ch.charAt(0);

}while(c!='n');

for(int i=0;i<v.size();i++)

{

Object temp=v.get(i);

String sale=temp.toString();

int salary=Integer.parseInt(sale);

if(salary>200 && salary<300)

count[0]++;

else if(salary<400)

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 20: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 20

count[1]++;

else if(salary<500)

count[2]++;

else if(salary<600)

count[3]++;

else if(salary<700)

count[4]++;

else if(salary<800)

count[5]++;

else if(salary<900)

count[6]++;

else if(salary<1000)

count[7]++;

else

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 21: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 21

count[8]++;

}

System.out.println(" ===============================================");

System.out.println(" ===============================================");

System.out.printf("\n%s%30s\n", "Salary Range","Number of Sales Person\n");

String range[] = new String[]{"$200-$299", "$300-$399", "$400-$499", "$500-$599", "$600-$699", "$700-$799", "$800-$899", "$900-$999", "$1000 and over "};

for( int i=0; i<count.length; i++ )

System.out.println( range[i] + "\t\t" + count[i]+"\n" );

System.out.println(" ===============================================");

System.out.println(" ===============================================");

}

}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 22: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 22

Output :-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 23: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 23

4. Write a version of a bubble sort that can be used to sort a string Vector object. Also, write a program to test your algorithm.

Program:-

import java.util.*;public class bubbleSort{ public static void main(String a[]){

int i; int array[] = {12,9,4,99,120,1,3,10};

System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println();

}

public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){

for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){

t = a[j-1];a[j-1]=a[j];a[j]=t;

} }

} }

}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 24: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 24

Output :-

5. Write a version of a selection sort that can be used to sort a string Vector object. Also, write a program to test your algorithm.

import java.util.*;public class SelectionSort{

public static void main(String a[]){

int i; int array[] = {12,9,4,99,120,1,3,10}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); selection_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println();

}

public static void selection_srt(int array[], int n){ for(int x=0; x<n; x++) { int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 25: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 25

index_of_min = y; }

} int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; } }}

Output :-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 26: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 26

6. Write a version of the insertion sort that can be used to sort a string Vector object. Also, write a program to test your algorithm.

import java.util.*;public class InsertionSorting{ public static void main(String a[]){

int i; int array[] = {12,9,4,99,120,1,3,10}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println();

insertion_srt(array, array.length);

System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println();

}

public static void insertion_srt(int array[], int n){ for (int i = 1; i < n; i++){

int loc = i; int temp = array[i]; while ((loc > 0) && (array[loc-1] > temp)){

array[loc] = array[loc-1]; loc--;

} array[loc] = temp;

} }}

Output :-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 27: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 27

TUTORIAL 10

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 28: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 28

1. Write the code to do the following(a) create an instance of a set(b) insert a new element into a set(c) remove an element from a set(d) find the size of a set

import java.util.*;class Person{ public static void main(String args[] ) {Set hs = new HashSet();hs.add(10);System.out.println("Size before removing element of hash set "+hs.size());hs.remove(10);System.out.println("Size after removing element "+hs.size());

Set ts = new TreeSet();ts.add(10);System.out.println("Size before removing element of tree set "+ts.size());ts.remove(10);System.out.println("Size after removing element "+ts.size()); }}

Output :-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 29: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 29

2. What are the differences between HashSet, Linked Hash Set and TreeSet?

TreeSet , LinkedHashSet and HashSet all are implementation of Set interface and by

virtue of that, they follows contract of Set interface i.e. they do not allow duplicate

elements.

Treeset provides natural sorting, linkedhashset maintains insertion order and hashset does

not maintain anything,also linkedhashset and hashset allows null but treeset does not as

when it compares using compareto() it will thow null exception.

HashSet doesn't provide any ordering. - add, delete, get - O(n) - constant time.

TreeSet provides ordering by using a comparator - add, delete, get - O(logn) - additional

cost of maintaining order compared to the HashSet. Internally maintains Red-black tree

for ordering. LinkedHashSet provides ordering based on insertion or access - add, delete,

get - O(1) but still provides ordering only for insertion/access based. Useful for building

LRU cache etc. Maintains double linked list internally to order elements. access based

ordering is costly.

3. Create two hash sets {“George”, “Jim”, “John”, “Blake”, “Kevin”, “Michael”} and {“George”, “Katie”, “Kevin”, “Michelle”} and find their union, difference and intersection.

import java.util.*;class Union{ public static void main(String []args) {Set s1 = new HashSet();s1.add("George");s1.add("Jim");s1.add("John");s1.add("Blake");s1.add("Kevin");s1.add("Michael");System.out.println(s1);Set s2 = new HashSet();s2.add("George");s2.add("Katie");s2.add("Kevin");s2.add("Michelle");System.out.println(s2);s1.addAll(s2);System.out.println(s1); }}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 30: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 30

Output :-

Program :-

import java.util.*;class difference{ public static void main(String []args) {Set s1 = new HashSet();s1.add("George");s1.add("Jim");s1.add("John");s1.add("Blake");s1.add("Kevin");s1.add("Michael");System.out.println(s1);Set s2 = new HashSet();s2.add("George");s2.add("Katie");s2.add("Kevin");s2.add("Michelle");System.out.println(s2);s1.removeAll(s2);System.out.println(s1); }}

Output:-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 31: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 31

Program :-

import java.util.*;class intersection{ public static void main(String []args) {Set s1 = new HashSet();s1.add("George");s1.add("Jim");s1.add("John");s1.add("Blake");s1.add("Kevin");s1.add("Michael");System.out.println(s1);Set s2 = new HashSet();s2.add("George");s2.add("Katie");s2.add("Kevin");s2.add("Michelle");System.out.println(s2);s1.retainAll(s2);System.out.println(s1); }}

Output :-

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 32: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 32

4. Write a program that reads words from user input and displays all the non-duplicate words in ascending order.

import java.*;class Word{ Public static void main(String args[]) {

int i, num;String Word;Scanner s = new Scanner (System.in)System.out.println(“Enter the number of words:”);num= s.nextInt();for (i=0; i<num; i++)Word = s.next();TreeSet ts = new TreeSet();System.out.println(Word);

}}

Output :-

5. Does the following program , TestHashSet.java, compile and run if line 7 (Set set = new HashSet()) is replaced by one of the following statements?

import java.util.*;

public class TestHashSet { public static void main(String[] args) { // Create a hash set Set<String> set = new HashSet<String>(); // Add strings to the set set.add("London"); set.add("Paris"); set.add("New York"); set.add("San Francisco");

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 33: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 33

set.add("Beijing"); set.add("New York");

System.out.println(set);

// Obtain an iterator for the hash set Iterator iterator = set.iterator();

// Display the elements in the hash set while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }

Output :-

a) Collection set = new LinkedHashSet();

Yes, the program is compiled and run. The output is:

b) Collection set = new TreeSet();

Yes, the program is compiled and run. The output is:

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 34: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 34

c) Collection set = new ArrayList();

Yes, the program is compiled and run. The output is:

d) Collection set = new LinkedList();

Yes, the program is compiled and run. The output is:

e) Collection set = new Vector();

Yes, the program is compiled and run. The output is:

f) Collection set = new Stack();

Yes, the program is compiled and run. The output is:

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 35: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 35

6. Suppose that set1 is a set that contains the strings "red", "yellow", "green", and that set2 is another set that contains the strings "red", "yellow", "blue". Answer the following questions:

a) What are set1 and set2 after executing set1.addAll(set2)?b) What are set1 and set2 after executing set1.add(set2)?c) What are set1 and set2 after executing set1.removeAll(set2)?d) What are set1 and set2 after executing set1.remove(set2)?e) What are set1 and set2 after executing set1.retainAll(set2)?f) What is set1 after executing set1.clear()?

Program :-

import java.util.*;class color{ public static void main(String []args) {Set set1 = new HashSet();set1.add("red");set1.add("yellow");set1.add("green");System.out.println("The items in set1 are "+set1);Set set2 = new HashSet();set2.add("red");set2.add("yellow");set2.add("blue");System.out.println("The items in set2 are "+set2);set1.addAll(set2);System.out.println("The set1 after set1.addAll(set2) is "+set1);System.out.println("The set2 after set1.addAll(set2) is "+set2);set1.add(set2);System.out.println("The set1 after set1.add(set2) is "+set1);System.out.println("The set2 after set1.add(set2) is "+set2);set1.removeAll(set2);System.out.println("The set1 after set1.removeAll(set2) is "+set1);System.out.println("The set2 after set1.removeAll(set2) is "+set2);set1.remove(set2);System.out.println("The set1 after set1.remove(set2) is "+set1);System.out.println("The set2 after set1.remove(set2) is "+set2);set1.retainAll(set2);System.out.println("The set1 after set1.retainAll(set2) is "+set1);System.out.println("The set2 after set1.retainAll(set2) is "+set2);set1.clear();System.out.println("The set1 after set1.clear() is "+set1);System.out.println("The set2 after set1.clear() is "+set2); }}

PT1381110 Asia Pacific Institute of Information Technology level 1

Page 36: INTRODUCTION TO SOFTWARE DEVELOPMENTkrishnakantkumar.weebly.com/uploads/4/3/8/5/43852107/isd.docx · Web viewIntroduction to Software Development (CE00371-1) Page | 2. PT1381110Asia

Introduction to Software Development (CE00371-1) P a g e | 36

Output :-

PT1381110 Asia Pacific Institute of Information Technology level 1