Você está na página 1de 16

SCHOOL OF INFORMATION TECHNOLOGY AND

ENGINEERING
LAB
Assessment - I

Course Name: Programming in JAVA


Course Code: SWE1008
Name: M.Deepshika
Register number: 18MIS0241
Slot: F1+TF1

1. Write a program called CozaLozaWoza which prints the numbers 1 to 110, 11


numbers per line. The program shall print "Coza" in place of the numbers
which are multiples of 3, "Loza" for multiples of 5, "Woza" for multiples of 7,
"CozaLoza" for multiples of 3 and 5, and so on. The output shall look like:

1 2 Coza 4 Loza Coza Woza 8 Coza Loza 11 Coza 13 Woza CozaLoza 16 17


Coza 19 Loza CozaWoza 22 23 Coza Loza 26 Coza Woza 29 CozaLoza 31 32
Coza

public class CozaLozaWoza


{
public static void main(String[] args)
{
CozaLozaWoza aCozaLozaWoza = new CozaLozaWoza();
aCozaLozaWoza.printCozaLozaWoza(110, 11);
}
private void printCozaLozaWoza(int itemsCount, int itemsPerLine)
{
boolean clwFlag;
for (int i = 1; i <= itemsCount; i++)
{
clwFlag = false;
if (i % 3 == 0) {
System.out.print("Coza");
clwFlag = true;
}
if (i % 5 == 0) {
System.out.print("Losa");
clwFlag = true;
}
if (i % 7 == 0) {
System.out.print("Woza");
clwFlag = true;
}
if ( ! clwFlag) {
System.out.print(i);
}
System.out.print(" ");

if (i % 11 == 0) {
System.out.println();
}
}
System.out.println();
}
}

2. Write a Java code to get two numbers and find the max among
them.

import java.util.Scanner;
public class Max
{
public static void main(String[] args)
{
int num1,num2;
Scanner inp=new Scanner(System.in);
System.out.println("Enter two numbers: ");
num1=inp.nextInt();
num2=inp.nextInt();
System.out.println("The maximum of the given two numbers is: ");
if(num1>num2)
{
System.out.println("The greater number is " + num1);
}
else
{
System.out.println("The greater number is " + num2);
}
}
}

3. Develop a Java program to get value for ‘n’ and print numbers
from 1 to ‘n’. In this case, make sure that the provided ‘n’ is a positive
number.

import java.util.Scanner;
public class Nprint
{
public static void main(String[] args)
{
int n;
Scanner inp=new Scanner(System.in);
System.out.println("Enter a number: ");
n=inp.nextInt();
while(n<0)
{
System.out.println("The given number is not positive!!!!! Enter a positive
number");
n=inp.nextInt();
}
System.out.println("The given number is positive:)");
System.out.println("The numbers from 1 to "+ n +" are printed below: ");
for(int i=1;i<=n;i++)
{
System.out.println(i);
}
}

}
4. Design a Java program to print the following pattern for the
positive value ‘n’.
Sample Output :

N=3 N=5
* *
*** ***
***** *****
*******
*********

import java.util.Scanner;
public class pattern
{
public static void main(String[] a)
{
int rows,k=0;
Scanner inp=new Scanner(System.in);
System.out.println("Enter the number of rows:");
rows=Integer.parseInt(inp.next());
for(int i=1;i<=rows;++i,k=0)
{
for(int space=1;space<=rows-i;++space)
{
System.out.print(" ");
}
while(k!=2*i-1)
{
System.out.print(" * ");
++k;
}
System.out.println();
}
}
}
5. Write a Java program that will read in month and day (as
numerical value). The program will return the equivalent zodiac sign.

Zodiac Sign Duration


Aquarius Jan 20 – Feb 18
Pisces Feb 19 – Mar 20
Aries Mar 21 – Apr 19
Taurus Apr 20 – May 20
Gemini May 21 – Jun 20
Cancer Jun 21 – Jul 22
Leo Jul 23 – Aug 22
Virgo Aug 23 – Sep 22
Libra Sep 23 – Oct 22
Scorpio Oct 23 – Nov 21
Sagittarius Nov 22 – Dec 21
Capricorn Dec 22 – Jan 19
[Sample output: Enter month: 6 Enter day: 25 Zodiac sign for June
25 is Cancer]

import java.util.Scanner;
public class zodiac
{
public static void main(String[] a)
{
int month,day;
Scanner inp = new Scanner(System.in);
System.out.println("Enter month :");
month = inp.nextInt();
System.out.println("Enter day :");
day = inp.nextInt();
String
m[]={"January","February","March","Arpil","May","June","July","August","Septembe
r","October","November","December"};
if((month==1 && day>=20) || (month==2 && day<=18))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Aquarius ");
}
if((month==2 && day>=19) || (month==3 && day<=20))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Pisces ");
}
if((month==3 && day>=21) || (month==4 && day<=19))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Aries ");
}
if((month==4 && day>=20) || (month==5 && day<=20))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Taurus ");
}
if((month==5 && day>=21) || (month==6 && day<=20))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Gemini ");
}
if((month==6 && day>=21) || (month==7 && day<=22))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Cancer ");
}
if((month==7 && day>=23) || (month==8 && day<=22))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Leo ");
}
if((month==8 && day>=23) || (month==9 && day<=22))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Virgo ");
}
if((month==9 && day>=23) || (month==10 && day<=22))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Libra ");
}
if((month==10 && day>=23) || (month==11 && day<=21))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Scorpio ");
}
if((month==11 && day>=22) || (month==12 && day<=21))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Sagittarius ");
}
if((month==12 && day>=22) || (month==1 && day<=19))
{
System.out.println("Zodiac sign for " + m[month-1] + " is Capricorn ");
}
}
}
6. Write Java code statements that accomplish the tasks listed below.
a) Declare an array of integers.
b) Allocate storage to allow 5 integers to be stored in the array.
c) Populate the array with the values: 1, 8, 27, 64, 125
d) Replace the third array element with the value -7.
e) Copy the value of the fifth array element to the first array
storage location.
f) Subtract the value stored in the second array storage location
from the value stored in the third and store the difference in the
fourth array storage location.
g) Compute the sum of the array elements with subscripts 1 to 3.
public class Arrayfunctions
{
public static void main(String[] args)
{
int sum,temp;
System.out.println("Declaring an array of integers.....");
int[] array;
System.out.println("Allocating storage to allow 5 integers to be stored in the
array.....");
array=new int[5];
System.out.println("Populating the array with the values.....");
array[0]=1;
array[1]=8;
array[2]=27;
array[3]=64;
array[4]=125;
System.out.println("The array is:");
for(int i=0;i<5;i++)
{
System.out.print(array[i]+"\t");
}
array[2]=-7;
System.out.print("\n");
System.out.println("The array after replacing the 3rd value with -7.....");
for(int i=0;i<5;i++)
{
System.out.print(array[i]+"\t");
}
array[0]=array[4];
System.out.print("\n");
System.out.println("The array after copying the fifth element to first array
location.....");
for(int i=0;i<5;i++)
{
System.out.print(array[i]+"\t");
}
array[3]=array[1]-array[2];
System.out.print("\n");
System.out.println("The array after subracting value in 2nd array storage
location from value in \n 3rd array storage location and storing it in 4th array storage
location.....");
for(int i=0;i<5;i++)
{
System.out.print(array[i]+"\t");
}
System.out.print("\n");
sum=array[1]+array[2]+array[3];
System.out.println("The sum of the array elements with subscripts 1 to 3 is : "
+sum);
}

}
7. Develop a class TelephoneIndex with two String objects as
members. One for should hold people’s name and the other should
hold their phone number. The class should have appropriate
constructor, input and display methods. Create an array of objects for
TelephoneIndex and do the following:
a) Your program should ask the user to enter a name or the first
few characters of a name to search for it in the array.
b) The program should display all of the names that match the
user’s input and their corresponding phone numbers.

import java.util.Scanner;
class TelephoneIndex
{
private final String name;
private final String phoneNumber;

public TelephoneIndex(String name, String phoneNumber)


{
this.name = name;
this.phoneNumber = phoneNumber;
}

public String getFirstNCharactersOfName(int n)


{
return name.substring(0, n%name.length());
}

@Override
public String toString()
{
return String.format("%20s %15s", name, phoneNumber);
}

public class TelephoneDirectory


{
public static void main(String[] args)
{
TelephoneIndex[] t = new TelephoneIndex[3];
t[0] = new TelephoneIndex("abc123", "9999999999");
t[1] = new TelephoneIndex("zxy987", "1111111111");
t[2] = new TelephoneIndex("abpqr567", "5555555555");

Scanner scanner = new Scanner(System.in);


System.out.println("first few chars of name to search? ");
String query = scanner.nextLine();
int queryLength = query.length();
for(int i=0; i<t.length; i++)
{
if(t[i].getFirstNCharactersOfName(queryLength).equals(query))
{
System.out.println(t[i]);
}
}

}
}

8. A class called MyPoint, which models a 2D point with x and y


coordinates, is designed as given below. It contains:
a) Two instance variables x (int) and y (int).
b) A "no-argument" constructor that constructs a point at (0, 0).
c) A constructor that constructs a point with the given x and y
coordinates.
d) A toString() method that returns a string description of the
instance in the format "(x, y)".
e) A method called distance(int x, int y) that returns the distance
from this point to another point at the given (x, y)coordinates.
f) An overloaded distance(MyPoint another) that returns the
distance from this point to the given MyPoint instance another
Write the code for the class MyPoint. Also write a test program
(called TestMyPoint) to test all the methods defined in the class.

import java.util.Scanner;
class MyPoint
{
int x,y;
MyPoint()
{
System.out.println("No-argument constructor.....");
x=0;
y=0;
}
MyPoint(int x1,int y1)
{
System.out.println("Parameterized constructor.....");
x=x1;
y=y1;
}
@Override
public String toString()
{
System.out.println("("+x+","+y+")");
return (null);
}
public double distance(int x,int y)
{
int xDiff=this.x-x;
int yDiff=this.y-y;
return (int) Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
public double distance(MyPoint another)
{
int xDiff=this.x-another.x;
int yDiff=this.y-another.y;
return (int) Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
}
public class program
{
public static void main(String[] args)
{
int a,b,c,d;
MyPoint p=new MyPoint();
Scanner inp = new Scanner(System.in);
System.out.println("Enter Two points:");
a = inp.nextInt();
b = inp.nextInt();
MyPoint p1=new MyPoint(a,b);
System.out.println("The given point is:");
p1.toString();
System.out.println("Enter another two points:");
c = inp.nextInt();
d = inp.nextInt();
System.out.println("The Distance Between the two given points is
:"+p1.distance(c, d));
System.out.println("The Distance Between the first point and the given points
is :"+p1.distance(5,6));
}
}
9. Design a Java code to receive input for ‘n’ numbers and sort
numbers and print.

import java.util.Scanner;
public class Nsort
{
public static void main(String[] args)
{
int n;
Scanner inp=new Scanner(System.in);
System.out.println("Enter the size of the elements to be sorted: ");
n=inp.nextInt();
int[] array=new int[n];
System.out.println("Enter the elements to be sorted: ");
for(int i=0;i<n;i++)
{
array[i]=inp.nextInt();
}
for (int j = 0; j < n; j++)
{
for (int k = j + 1; k < n; k++)
{
if (array[j] > array[k])
{
int temp;
temp = array[j];
array[j] = array[k];
array[k] = temp;
}
}
}
System.out.println("Enter the numbers in sorted order is: ");
for(int i=0;i<n;i++)
{
System.out.println(array[i]);
}
}
}

Você também pode gostar