0801CS211029Assignment#1
0801CS211029Assignment#1
Code:
OUTPUT:
Q2. Write/Get a program in Java to add two numbers. Try to understand the program as above.
CODE:
OUTPUT:
Q3. Write a Java program to accept a sequence of positive numbers and print its average.
List will be terminated by -1.
CODE:
OUTPUT:
Q4. Modify above program or otherwise, make provision that if user enters S instead of number then display
current average, if user enters C then clear the values entered till now, and start afresh.
CODE:
import java.util.*;
public class Lab1Q4 {
static void average2() {
Scanner input = new Scanner(System.in);
int sum = 0;
int element = 0;
int counter = 0;
System.out.println("Please Enter the numebrs");
while (true) {
System.out.println("Number: ");
element = input.nextInt();
System.out.println("Enter S means get aversge or C means delete
number and start from afresh");
input.nextLine();
System.out.println("String: ");
String s = input.nextLine();
counter++;
sum = sum + element;
if (s.equals("S")){
if (counter == 0) System.out.println(-1);
else
System.out.println("average: "+((float)sum / counter));
break;
}
else if(s.equals("C")){
sum =0;
counter=0;
continue;
}
}
}
public static void main(String[] args) {
Lab1Q4 obj = new Lab1Q4();
obj.average2();
}
}
OUTPUT:
Q5. Modify or design a new program for following:
Handle three lists simultaneously.
The lists are l1,l2,l3.
By default operations are on l1.
User can select the list by entering L1 or L2 or L3.
CODE:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Lab01Q5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
list L1 = new list();
list L2 = new list();
list L3 = new list();
String condition = listWorker(L1);
while(true){
if(condition.equals("L1")){
condition= listWorker(L1);
}
else if(condition.equals("L2")){
condition=listWorker(L2);
}
else{
condition= listWorker(L3);
}
System.out.println(L1.Lists.toString());
System.out.println(L2.Lists.toString());
System.out.println(L3.Lists.toString());
break;
}
}
}