Vous êtes sur la page 1sur 26

ASSESSMENT – 1

16BIT0326 SURYA VAMSHI

1. Read the radius and print the area of a circle

code:-

import java.util.*;

public class area {


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int radius = sc.nextInt();
double area = Math.PI*radius*radius;
System.out.println(area);
sc.close();
}
}

output:-
2. Read the number and check whether it is divisible by 3 and 5.

code:-
import java.util.*;

public class div{


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n%3 == 0 && n%5 == 0){
System.out.println("It is divisible by 3 and 5");
}
else{
System.out.println("It is not divisible by 3 and 5");
}
sc.close();
}
}

output:-
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.*;

public class subject{


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n == 604){
System.out.println("Java Programming");
}
else if(n == 605){
System.out.println("Python Programming");
}
else{
System.out.println("Sorry! invalid input");
}
sc.close();
}
}

output:-
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

code:-
import java.util.*;

public class sum{


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = (n*(n+1))/2;
System.out.println(sum);
sc.close();
}
}

output:-
2 2 2
5. Print the sum of the series 1 +2 +3 up to n terms

code:-
import java.util.*;

public class sum2{


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = (n*(n+1)*(2n+1))/6;
System.out.println(sum);
sc.close();
}
}

output:-
6. Print the multiplication table by getting the n from the user.

Code:-
import java.util.*;

public class mul{


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1;i<=20;i++){
int mul = n*i;
System.out.println(n+" * "+i+" = "+mul);
}
sc.close();
}
}

output:-
7. Provide the option of adding two numbers to the user until the user wants
to exit.

Code:-
import java.util.*;

public class usersum{


public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=1;
while(t!=0){
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("sum = "+(a+b));
t--;
System.out.println("Continue Yes/No");
String s=sc.next();
if(s.equals("Yes")){
t++;
}
}
sc.close();
}
}

output:-
8. Print this pattern for n lines

(a) code:-
import java.util.*;

public class pattern1{


public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}

output:-
(b) code:-
import java.util.*;

public class pattern2{


public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=n;i>0;i--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
sc.close();
}
}

output:-
(c) code:-
import java.util.*;

public class pattern3{


public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
for(int i=n;i>0;i--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
sc.close();
}
}

output:-
9. 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.

Code:-
import java.util.*;

class sort{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n; i++){
arr[i] = sc.nextInt();
}
for(int i = 0; i<n-1; i++){
for(int j = 0; j<n-1; j++){
if(arr[j]>arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
System.out.print("Sorted array: ");
for(int i = 0; i<n; i++){
if(i%2==0){
System.out.print(arr[n-(i/2)-1] + " ");
}
else{
System.out.print(arr[((i+1)/2)-1]+ " ");
}
}
System.out.println();
}
}

output:-
10. Write a Java program to separate even and odd numbers of an given array of
integers. Put all even numbers first, and then odd numbers.

Code:-
import java.util.*;

class eo{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i =0; i<n; i++){
arr[i] = sc.nextInt();
}
int[] a = new int[n];
int left = 0, right = n-1;
while(left<right){
while(arr[left]%2==0 && left<right){
left++;
}
while(arr[right]%2==1 && left<right){
right--;
}
if(left<right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
for(int x:arr){
System.out.print(x+" ");
}
System.out.println();
}
}

output:-

11. Write a Java program to remove the duplicate elements of a given array and
return the new length of the array.

Code:-
import java.util.Scanner;

public class RemoveDuplicateElementFromArray{


public static void main(String[] args){
/* Array instantiation */
int[] arr_elements = new int[20];

/* initial_element variable initialize by 0 and


point to the first element of the array */
/* next_element points to next element of array */
int initial_element,next_element;
int i;

/*Create Scanner Object */


Scanner sc = new Scanner(System.in);
/*Display array size for user*/
System.out.print("Enter array size: ");
int arr_size = sc.nextInt();

/*Display message for array element*/


System.out.println("Read Array Elements From User :");

/*Loop to take input array elements*/


for(i=0;i<arr_size;++i)
{
System.out.print("Enter array elements of index " +i +": ");
arr_elements[i] = sc.nextInt();
}

/* Display array before removing duplicate element */


System.out.println("Before removing duplicate element array are
:");

/* Loop for displaying array elements */


for(i=0;i<arr_size;++i)
{
System.out.println(arr_elements[i]);
}

/* Get new line


System.out.println();
/* Display array after removing duplicate array element */
System.out.println("After removing duplicate element array are :");
for(initial_element=0;initial_element<arr_size;++initial_element)
{

for(next_element=initial_element+1;next_element<arr_size;){
/* if initial_element matches to next_element
then take next _element and matches till end */
if(arr_elements[initial_element] ==
arr_elements[next_element]){
for(int temp = next_element; temp<arr_size;
++temp){
arr_elements[temp] =
arr_elements[temp+1];
}
arr_size = arr_size-1;
}
else
next_element++;
}
}

/* Loop to display array after removing duplicate element */


for(i=0;i<arr_size;++i)
System.out.println(arr_elements[i]);
}
}

Output:-
12. Write a Java program to find the sum of the two elements of a given array
which is equal to a given integer.

Code:-
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;

public class PairsHavingGivenSum {


// Function to print the pair of elements having a given sum
static void printPairs(int[] array,int sum)
{
HashMap<Integer,Integer> obj=new HashMap<>();
int i,search;
System.out.println("The pairs having sum "+sum+" are");
for(i=0;i<array.length;i++){
search=sum-array[i];
if(obj.containsValue(search)){
System.out.println(array[i]+" and "+search);
}
else
{
obj.put(i,array[i]);
}
}
}
// Main function to read the input
public static void main(String[] args) {
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try{
size=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("Invalid Input");
return;
}
int[] array=new int[size];
System.out.println("Enter array elements");
int i;
for(i=0;i<array.length;i++){
try{
array[i]=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("Invalid element. Enter it again");
i--;
}
}
int sum;
System.out.println("Enter the sum you want to look for");
try{
sum=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("Invalid Input");
return;
}
printPairs(array,sum);
}
}

Output:-
13. Write a program to demonstrate the knowledge of students in
multidimensional arrays and looping constructs.

Code:-
class Main
{
public static void main(String[] args)
{
// Declaring 2-D array with 2 rows
int arr[][] = new int[2][];

// Making the above array Jagged

// First row has 3 columns


arr[0] = new int[3];

// Second row has 2 columns


arr[1] = new int[2];

// Initializing array
int count = 0;
for (int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++)
arr[i][j] = count++;

// Displaying the values of 2D Jagged array


System.out.println("Contents of 2D Jagged Array");
for (int i=0; i<arr.length; i++)
{
for (int j=0; j<arr[i].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}

Output:-
14. A hash algorithm uses rotation and fold shift methods to compute the
address at which the user input has to be stored. Define a static method to
perform rotation of the data by moving the least significant digit to the most
significant bit position. Also define a non-static method to perform fold shift by
dividing the rotated data into segments of length 2 and then add all the segments
to get the hash address. If the sum has more than 2 digits, delete the most
significant digit and the resulting number is the address. Invoke these methods
from main( ) method.
Eg., If the data is 112286, after rotation it should be 611228 and after fold shift
it should be 61 + 12 + 28=101 =01 (after deleting the most significant digit)

Code:-
import java.util.*;
public class rotate {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char[] arr=new char[s.length()];
for(int i=0;i<s.length();i++){
if(i==0){
arr[i]=s.charAt(s.length()-1);
}
else{
arr[i]=s.charAt(i-1);
}
}
String rot=new String(arr);
String[] str=new String[s.length()/2];
int sum=0;
int count=0;
for(int i=0;i<s.length();i++){
str[count]=rot.substring(i,i+2);
int x=Integer.parseInt(str[count]);
sum=sum+x;
count++;
i++;
}
if(sum>99){
int d=sum/10;
System.out.print(d%10);
System.out.println(sum%10);
}
else{
System.out.println(sum);
}
}
}

Output:-

15. Consider a Java program containing a statement to invoke format( ) method


for displaying the output. Write a program to verify the syntax correctness of
the statement by checking for the following.

- The number of format specifiers and arguments should match.


- Datatype of the arguments should match the format specifiers used.

Code:-
code:-
import java.util.*;
public class format{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int xint=str.indexOf("%d");
int xfloat=str.indexOf("%f");
int xstring=str.indexOf("%s");
int a=str.indexOf(",a");
int b=str.indexOf(",b");
int s=str.indexOf(",s");
int x=str.indexOf(",x");
int count=0;
if(xint > xfloat){
if(a<b || x<b){
count++;
}
}
else if(xint < xfloat){
if(a>b || x>b){
count++;
}
}
if(xint > xstring){
if(a<s || x<s){
count++;
}
}
else if(xint < xstring){
if(a>s || x>s){
count++;
}
}
if(xfloat > xstring){
if(b<s){
count++;
}
}
else if(xfloat < xstring){
if(b>s){
count++;
}
}
if(count==0){
System.out.println("correct syntax");
}
else{
System.out.println("wrong syntax");
}
}
}
Output:-

16. 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.

Code:-
public class Main {

public static void main(String[] args) {

int i = 0;
int j = 0;
int k = 0;
int l = 0;

Student st = new Student("Braun", "Thiago", 1);

System.out.println("Student Last Name is:" + " " + st.getLastName());


System.out.println("Student First Name is:" + " " + st.getFirstName());
System.out.println("Student ID is:" + " " + st.getStudentId());

while (st.getProjectScore(st.getNextProjectIndex()) == -1.0 && i <=14){


st.setProjectScore(70.0, 0);
st.setProjectScore(70.0, 1);
st.setProjectScore(70.0, 5);
st.setProjectScore(70.0, 9);
st.setProjectScore(70.0, 10);
i++;
}

while (st.getQuizScore(st.getNextQuizIndex()) == -1.0 && j <=14){


st.setQuizScore(70.0, 0);
st.setQuizScore(70.0, 1);
st.setQuizScore(70.0, 5);
st.setQuizScore(70.0, 8);
st.setQuizScore(70.0, 9);
j++;
}

do {
System.out.println("Student Project Grades are:" + " " +
st.getProjectScore(k));
k++;
} while (k <= 14);

do {
System.out.println("Student Quiz Grades are:" + " " + st.getQuizScore(l));
l++;
} while (l <= 9);
17. 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.
(iii)A method named getArea() that returns the area of this rectangle.
(iv)A method named getPerimeter() that returns the 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 RectangleDemo {
private class Rectangle {
private double height;
private double width;
private String color;
public Rectangle(double wid, double high){
height = high;
width = wid;
}
public Rectangle(){
height = 1;
width = 1;
color = "White";
}
public void setHeight(double high){
height = high;
}
public void setWidth(double wid){
width = wid;
}
public void setColor(String col){
color = col;
}
public double getArea(){
return height*width;
}
public double getPerimeter(){
return 2*(height + width);
}
public void getColor(){
System.out.println("Color is: " + color +"\n");
return;
}
}

18. Write a Java program that displays that displays the time in different formats
in the form of HH,MM,SS using constructor Overloading.

Code:-
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SimpleDateFormatExample {


public static void main(String[] args) {
Date curDate = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

String DateToStr = format.format(curDate);


System.out.println(DateToStr);

format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");


DateToStr = format.format(curDate);
System.out.println(DateToStr);

format = new SimpleDateFormat("dd MMMM yyyy zzzz",


Locale.ENGLISH);
DateToStr = format.format(curDate);
System.out.println(DateToStr);

format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");


DateToStr = format.format(curDate);
System.out.println(DateToStr);

try {
Date strToDate = format.parse(DateToStr);
System.out.println(strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Output:-

Vous aimerez peut-être aussi