0% found this document useful (0 votes)
15 views

0801CS211029Assignment#1

The document outlines a lab assignment for a student named Ayush Mishra, consisting of five programming tasks in Java. The tasks include printing 'Hello World', adding two numbers, calculating the average of a sequence of positive numbers, and modifying a program to handle user inputs for average calculation and list management. Additionally, the document includes code snippets and expected outputs for each task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

0801CS211029Assignment#1

The document outlines a lab assignment for a student named Ayush Mishra, consisting of five programming tasks in Java. The tasks include printing 'Hello World', adding two numbers, calculating the average of a sequence of positive numbers, and modifying a program to handle user inputs for average calculation and list management. Additionally, the document includes code snippets and expected outputs for each task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 01 Assignment

Name : AYUSH MISHRA


Enrollment number : 0801CS211029
Q.1) Write or get a program in Java to print "Hello World".
Check parts of the program to see if intent of any portion is not clear. Find out its meaning and
rough information.

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("want to print the lists -1 for that


other wise write 1");
int exit = input.nextInt();
if(exit==-1){

System.out.println(L1.Lists.toString());
System.out.println(L2.Lists.toString());
System.out.println(L3.Lists.toString());
break;
}

}
}

static String listWorker(list list_object){


Scanner input = new Scanner(System.in);
int a = input.nextInt();
list_object.Lists.add(a);

System.out.println("Enter the list that you want to use L1 or


L2 or L3");
String buffer = input.nextLine();
String switch1 = input.nextLine();
return switch1;
}
static class list{
List<Integer> Lists = new ArrayList<>();
}
}
OUTPUT:

You might also like