Java Programming Lab Assignment 1: Basic Programs
Java Programming Lab Assignment 1: Basic Programs
LAB ASSIGNMENT 1
BASIC PROGRAMS
Code:
import java.util.Scanner;
class f1{
double pi=3.14, a;
int r=sc.nextInt();
a= pi*r*r;
System.out.println("The area of the circle is"+a);
}
}
Output:
code
class q {
3. Display Subject Name based on room number. If the user enters 604 then display
Java Programming , If the user enters 605 then display Python programming for any
other input display Invalid input to the user
Code
import java.util.*;
class f3
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n==604)
{
System.out.println("java programming");
elseif(n==605){
System.out.println("python programming");
else{
System.out.println("Invalid Input");
4. Print the sum of first n numbers. If n is 3 then print the sum of 1+2+3 to the user. Get n from
the user
import java.util.Scanner;
public class naturalnum {
num = scan.nextInt();
//closing scanner after use
scan.close();
for(count = 1; count <= num; count++){
total = total + count;
}
System.out.println("Sum of first "+num+" natural numbers is: "+total);
}
}
5. Print the sum of the series 1^ 2 +2^ 2 +3^ 2 up to n terms 6. Print the multiplication table by
getting the n from the user.
code:
import java.util.Scanner;
}
}
code:
import java.util.Scanner;
class mul
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of " + n);
Code:
port java.util.Scanner;
public class add
{
public static void main(String[] args)
{
int m, n, opt, add;
double div;
Scanner s = new Scanner(System.in);
System.out.print("Enter first number:");
m = s.nextInt();
System.out.print("Enter second number:");
n = s.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 to Exit");
opt = s.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;
case 5:
System.exit(0);
}
}
}
}
System.out.println(" ");
}
}
}
b)
import java.util.Scanner;
System.out.println("");
System.out.println("Number of rows:");
System.out.println("Output pattern");
System.out.println();
}
//Printing lower half of the pattern
System.out.println();
}
sc.close();
}
}
Exercise programs Based on Array
1. Write a Java program to sort an array of positive integers of an given array, in the sorted array
the value of the first element should be maximum, second value should be minimum value,
third should be second maximum, fourth second be second minimum and so on.
import java.util.Scanner;
int b=0;
int n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]<a[i])
{
b=a[j];
a[j]=a[i];
a[i]=b;
}
}
}
for(int i:a)
{
System.out.println("The sorted array is"+i);
}
}
}
2. Write a Java program to separate even and odd numbers of a given array of integers. Put all
even numbers first, and then odd numbers.
// Java program to segregate even and odd elements of array
import java.io.*;
import java.util.*;
class evenodd
{
static void segregateEvenOdd(int arr[])
{
/* Initialize left and right indexes */
int left = 0, right = arr.length - 1;
while (left < right)
{
/* Increment left index while we see 0 at left */
while (arr[left]%2 == 0 && left < right)
left++;
segregateEvenOdd(arr);
System.out.print(" ");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i]+" ");
}
}
3. Write a Java program to remove the duplicate elements of a given array and return the
new length of the array
public class RemoveDuplicate{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1])
{ temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
// Changing original array
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
4. Write a Java program to find the sum of the two elements of a given array which is equal to a
given integer.
import java.util.*;
public class sumg
{
public static ArrayList<Integer> two_sum_array_target(final List<Integer> a, int b) {
return result;
}
import java.util.*;
import java.io.*;
public class sumrow
{
public static void main(String[] args) {
int rows, cols, sumRow, sumCol;
int a[][] = {
{87, 90, 71},
{79, 54, 58},
{73, 80, 94}
};
System.out.print("\n");
}
}
}
8. Write a program to demonstrate the knowledge of students in multidimensional arrays and
looping constructs. Eg., If there are 4 batches in BTech - “CSE1007” course, read the count of the
slow learners (who have scored
importjava.util.Scanner;
int i, j;
double t;
if(t >= 4)
arr[i][j] = 4;
else
arr[i][j] = (int)t;
t = t - 4;
sc.close();
int cfour = 0;
if(arr[i][j] == 4)
cfour++;
System.out.println();
}
}
PROGRAMS BASED ON CLASSES AND OBJECTS
1. Design a class named Rectangle to represent a rectangle. The class contains: Two
double data fields named width and height that specify the width and height of the
rectangle. The default values are 1 for both width and height.
(i)A default constructor that creates a default rectangle.
(ii)A constructor that creates a rectangle with the specified width and
height.
perimeter.
Implement the class. Write a test program that creates two Rectangle objects—one
with width 5 and height 50 and the other with width 2.5 and height 45.7. Display
the width, height, area, and perimeter of each rectangle in this order
CODE:
public class Rectangle {
// variable declarations
private double height;
private double width;
// default constructor
public Rectangle(double wid, double high){
height = high;
width = wid;
}
// parametrized constructor
public Rectangle(){
height = 1;
width = 1;
}
// return area of rectangle
}
// returns parameter of a rectangle
public double getPerimeter()
{ return 2*(height + width);
}
// returns width
public double getWidth(){
return width;
}
// returns height
public double getHeight(){
return height;
}
public static void main(String[] args){
// creating objects
Rectangle box1 = new Rectangle(4, 40);
Rectangle box2 = new Rectangle(3.5, 35.9);
// calling methods
System.out.println("Width: " + box1.getWidth());
2. Write a Java program to create a class called Student having data members Regno,
Name, Course being studied and current CGPA. Include constructor to initialize
objects. Create array of objects with at least 10 students and find 9-pointers.
class ObjectArray{
public static void main(String args[]){
obj[i]=new Student();
}
obj[0].setreg(1);
obj[1].setcourse("CSE2001");
obj[2].setcgpa(8.5);
System.out.println("For Array Element 0");
obj[0].showData();
System.out.println("For Array Element
1"); obj[1].showData();
}
}
class Student{
int x;
String y;
String w;
int v;
public void setregno(int c){
x=c;
public void setcourse(String b){
y=b;
3. Write a Java program that displays that displays the time in different formats in
the form of HH,MM,SS using constructor Overloading.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CurrentTimeDateCalendar {
}
}