Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
138 views24 pages

Name: Soumik Paul Class: XI Roll No.: 30 Stream: Science Subject: Computer Science

The document contains 11 programming assignments with sample code solutions in Java. The assignments cover topics like calculating a telephone bill, finding the average and deviation of student marks, shifting positive and negative numbers in an array without changing their order, finding the sum of prime numbers, generating prime numbers, summing two binary numbers, inheritance with Rectangle class extending Conversion class, and displaying cities and their STD codes. The document includes an introduction to Java fundamentals and an acknowledgement section thanking those who helped with the project.

Uploaded by

Sanchita Kundu
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)
138 views24 pages

Name: Soumik Paul Class: XI Roll No.: 30 Stream: Science Subject: Computer Science

The document contains 11 programming assignments with sample code solutions in Java. The assignments cover topics like calculating a telephone bill, finding the average and deviation of student marks, shifting positive and negative numbers in an array without changing their order, finding the sum of prime numbers, generating prime numbers, summing two binary numbers, inheritance with Rectangle class extending Conversion class, and displaying cities and their STD codes. The document includes an introduction to Java fundamentals and an acknowledgement section thanking those who helped with the project.

Uploaded by

Sanchita Kundu
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/ 24

1

Name : Soumik Paul


Class : XI
Roll No. : 30
Stream : Science
Subject : Computer Science
2

Index

Serial No. Program Name/Class Name Page no.


1 Acknowledgement 3
2 Introduction 4
3 telephoneBill 5
4 studentMarkAndAvg 8
5 posNegArrayShift 10
6 primeSumCall 12
7 GeneratePrime 14
8 sumOf2BinaryNums 16
9 Rectangle extends Conversion 18
10 citiesAndSTDs 21
11 Conclusion 23
12 Bibliography 24
3

Acknowlegdement
I am really grateful to all those who have helped me in doing this project successfully. Special thanks to our
subject teacher Arup sir for choosing such beautiful conceptual programs for the students. He has taught us such
conceptual programs so that we could secure good marks in the upcoming board exams. I am very thankful to
him for helping me in the pandemic situation, where we could not have physical classes.I am also grateful to my
mother for helping me in decorating this project to make it more attractive and look better. Similarly, the
contribution of my friends in the project is quite praiseworthy. They encouraged me to do this project even better
than it could be.

Mr. Arup Sarkar


Subject Teacher of Computer Science
4

Introduction
In any language, there are some fundamentals that you need to know before you can write even the
most elementary programs. We are introduced to Java fundamentals to you so that you may start
writing your own Java programs. We will be learning about Java tokens such as keywords, identifiers,
literals, operators and separators, along with data types, variables, constants, operators, expressions.
You'll also be learning about how a class forms the basis of all computation in Java. Let us begin our
discussion with Java characters set and tokens.
Java is a powerful high level language and you can write your programs in it depending upon
your requirements. With its powerful functionality, it has become a very popular language among the
programmers. The basis of Java programming are objects and classes.
A Class is a specification for objects and as an object factory. A class is called a basic part of a
Java application. Without declaring a class and its main method, it is not possible to do programming in
Java. Without data types, programming is not possible. Java provides you various in-built data types.
These in-built data types are called primitive data types. There are 8 primitive data types-bytes, short,
int, long, float, double, char and boolean. You can create your own data types in Java programs by
using primitive data types.
To write any type of program in Java, certain things are required in addition to Values and Data
Types. These include operators and expressions. Java effectively handles input operations. For input
operations, you are already familiar that Buffered Reader class is used, but input operations are handled
by Java using Scanner class.
Java provides powerful functions for mathematical calculations making the work of
computations easy. The java.lang package is full of mathematical functions. While calculating through
the use of expression and mathematical one lava mak functions, Java makes use of this package. Java
language offers a Java Development Kit for development of the programs.
Using this kit, various interfaces like BlueJ and Greenfoot have been developed to develop the program
using Java features in an efficient manner.
5

Assignment

1. You are to print the telephone bill of a subscriber. Create a class having the following data members: Phone
Number of long data type (for storing the phone number). Name of String type (for storing the name of the
subscriber). Hire Charge a symbolic constant of int type (to store monthly hire charge say Rs. 200). Units
Consumed of int type (to store the monthly units consumed by the subscriber). Bill Amount of float type (to
store the bill amount that is payable). Create member functions for the following:
i. Constructor to initialise all data members except Hire Charge and Bill Amount.
ii. Calculate the bill amount payable which is Hire Charge+(Rs.. 1 per unit for the first 100 units, Rs.. 1.50 per
unit for the next 100 units and Rs.. 2.00 per unit thereafter.
iii. To display the Bill for the subscriber.

Code :

import java.util.*;

class telephoneBill
{
long phoneNumber;
String name;
int hireCharge=200,units;
float billAmount;

telephoneBill(String n,long phno,int u)


{
name=n;
phoneNumber=phno;
units=u;
}

void calculate()
{
6

billAmount=hireCharge;

if(units<=100)
billAmount+= (100-units)*1;
else if(units>100 && units<=200)
billAmount+=100*1 + (units-200)*1.5;
else
billAmount+=100*1 + 100*1.5 + (units-300)*2;
}

void display()
{
System.out.println("===================================");
System.out.println("Name : "+name);
System.out.println("Phone Number : "+phoneNumber);
System.out.println("Units Consumed : "+units);
System.out.println("Amount Payable : "+billAmount);
}

static void main()


{
Scanner sc = new Scanner(System.in);

System.out.println("Enter name :");


String n2=sc.nextLine();
System.out.println("Enter Mobile number :");
long phno2=sc.nextLong();
System.out.println("Enter Units consumed :");
int u2=sc.nextInt();
7

telephoneBill ob = new telephoneBill(n2,phno2,u2);


ob.calculate();
ob.display();
}
}

Output
8

2. Write a program to accept name and total marks of N number of students in two single subscript array name[]
and totalmarks[].
Calculate and print:
i. The average of the total marks obtained by N number of students.
ii. Deviation of each student’s total marks with the average. [deviation=total marks of a student - average].

Code :
import java.util.*;

class studentMarksAndAvg

static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the no. of students :");

int N=sc.nextInt();

String name[]=new String[N];

int totalmarks[]=new int[N];

double avg=0;

for(int i=0;i<N;i++)

System.out.println("Enter name of Student "+(i+1)+" :");

name[i]=sc.next();

System.out.println("Enter mark of Student "+(i+1)+" :");

totalmarks[i]=sc.nextInt();

avg+=totalmarks[i];

avg/=N;
9

System.out.println("==============================");

System.out.println("Average Marks : "+avg);

for(int i=0;i<N;i++)

System.out.println("Deviation of Student No. "+(i+1)+" : "+(totalmarks[i]-


avg));

Output
10

3. Write a program to input 10 positive or negative numbers (no zero) into an integer array and shift all positive
numbers to the beginning of the array and negative numbers to the end of the array; without changing the order
of the numbers.
For example, if the array contains the following elements:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
-9 12 - 3 -7 89 -34 15 16 -67 25
After shifting the array should contain the elements arranged in the following manner:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
12 89 15 16 25 -9 -3 -7 -34 -67
Positive numbers………………….. Negative number……………………..

Code :

import java.util.*;

class posNegArrayShift

static void main()

Scanner sc=new Scanner(System.in);

int a[]=new int[10],n=0;

System.out.println("Enter 10 numbers :");

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

for(int i=0,t=0;i<10;i++)

for(int j=0;j<9;j++)

{
11

if(a[j]<0 && a[j+1]>0)

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

System.out.print("Modified Array : ");

for(int i=0;i<10;i++)

System.out.print(a[i]+" ");

Output
12

4. Create a class with the following methods:


a. boolean prime(int n), which returns true if n is prime otherwise returns false.
b. int sum(int n), which finds the sum of the digits in n and returns it.
c. void call() to display all such three digit numbers, whose sum of the digits is a prime number.

Code :

import java.util.*;

class primeSumCall

boolean prime(int n)

int f=0;

for(int i=1;i<=n;i++)

if(n%i==0)

f++;

if(f==2)

return true;

else

return false;

int sum(int n)

int s=0;

for(;n>0;n/=10)

s+=(n%10);

return s;

}
13

void call()

for(int i=100;i<=999;i++)

if(prime(sum(i)))

System.out.println(i+" ");

static void main()

primeSumCall ob=new primeSumCall();

ob.call();

Output

(*The following output is saved from terminal window and further aligned in Notepad)
14

5. Create a class called GeneratePrime which will be used to generate n number of prime numbers. The class
should have the following methods:
(i) Method called isPrime() which accepts an integer as a parameter and return true if it is a prime number
otherwise return false.
(ii) Method called display() which accepts an integer n as Scanner input and display the first n prime number by
calling the above function.

Code :

import java.util.*;

class GeneratePrime

boolean isPrime(int x)

int f=0;

for(int i=1;i<=x;i++)

if(x%i==0)

f++;

if(f==2)

return true;

else

return false;

void display()

Scanner sc=new Scanner(System.in);

System.out.println("Enter number of terms :");

int n=sc.nextInt();
15

int i=0,x=1;

System.out.println("First "+n+" prime numbers are : ");

while (i<n)

if(isPrime(x++))

System.out.print((x-1)+" ");

i++;

static void main()

GeneratePrime ob = new GeneratePrime();

ob.display();

Output
16

6. Create a class with the following functions:


(i) boolean isBinary() which accepts a binary number as parameter and return true if it is a valid binary number
or not. A binary number consists of only two digits 0 and 1.
(ii) int binToDecimal(int b) which accepts a binary number as parameter and return its decimal equivalent.
(iii) int deciToBinary(int d) which accepts a decimal number as parameter and return its binary equivalent.
(iv) void sum() where you input two binary numbers and if they are valid binary numbers find their sum. Sample
input and output when the sum() function is executed:
INPUT Enter two valid binary numbers: 1011 , 11011
OUTPUT Sum of the two given binary numbers is: 100110.

Code :
import java.util.*;

class sumOf2BinaryNums

boolean isBinary(int n)

boolean x=true;

for(;n>0;n/=10)

if(!(n%10==0 || n%10==1))

x=false;

return x;

int binToDecimal(int b)

int d,n;

for(d=0,n=0;b>0;n++,b/=10)
17

d+=(b%10)*Math.pow(2,n);

return d;

void sum()

Scanner sc=new Scanner(System.in);

System.out.println("Enter two binary numbers :");

int n1=sc.nextInt(),n2=sc.nextInt();

if(isBinary(n1) && isBinary(n2))

int deciSum=binToDecimal(n1)+binToDecimal(n2);

System.out.println("Binary Sum is : "+(Integer.toBinaryString(deciSum)));

else

System.out.println("Invalid Binary Number");

static void main()

sumOf2BinaryNums ob=new sumOf2BinaryNums();

ob.sum();

Output
18

7. Define a class named Conversion having the following static methods:


i. int mTocm(int m ), which converts metre(m) to centimeter and return it.
ii. int multiply(int x, int y ), which returns the product of x and y.

Define another class named Rectangle which contains the following data members:
• length of int type which holds the length of a rectangle in meter.
• breadth of int type which holds the breadth of a rectangle in centimeter.

Create member functions:


i. Constructor to initialize the data members.
ii. Convert the length to centimeter by calling the relevant method of the above class.
iii. Compute the area by calling the relevant method of the above class and display the result.

Code :

class Conversion

int mTocm(int m)

return m*100;

int multiply(int x, int y)

return x*y;

import java.util.*;

class Rectangle extends Conversion

int length,breadth;
19

Rectangle(int l,int b)

length=l; //in metre

breadth=b;

void convertLengthToCm()

length=mTocm(length);

void area()

int area=multiply(length,breadth);

System.out.println("Area of rectangle : "+area+" cm^2");

void display()

System.out.println("The length of rectangle : "+length+" cm");

System.out.println("The Breadth of Rectangle : "+breadth+" cm");

static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter length(in m) and breadth(in cm) of rectangle : ");

int l=sc.nextInt(),b=sc.nextInt();

Rectangle ob = new Rectangle(l,b);


20

ob.convertLengthToCm();

ob.display();

ob.area();

Output
21

8. Write a program to accept the names of 10 cities in a single dimension string array and their STD (Subscribers
Trunk Dialing) codes in another single dimension integer array. Search for a name of a city input by the user in
the list. If found, display “Search Successful” and print the name of the city along with its STD code, or else
display the message “Search Unsuccessful, No such city in the list’.
Code :
import java.util.*;

class citiesAndSTD

static void main()

Scanner sc=new Scanner(System.in);

String cities[]=new String[10];

int STDCode[]=new int[10];

int i;

boolean x=false;

for(i=0;i<10;i++)

System.out.println("Enter names followed by STD Codes of City "+(i+1)+" :");

cities[i]=sc.next();

STDCode[i]=sc.nextInt();

System.out.println("Enter name of the city to be searched :");

String c=sc.next();

for(i=0;i<10;i++)

if(c.equalsIgnoreCase(cities[i]))

{
22

x=true;

break;

if(x)

System.out.println("Search successful !");

System.out.println("STD Code : "+STDCode[i]);

else

System.out.println("Search Unsuccessful, No such city in the list");

Output
23

Conclusion
Java offers the real possibility that most programs can be written in a type-safe language. However, for Java to
be broadly useful, it needs to have more expressive power than it does at present. It extends Java with a
mechanism for parametric polymorphism, which allows the definition and implementation of generic
abstractions. The paper gives a complete design for the extended language. The proposed extension is small and
conservative and the paper discusses the rationale for many of our decisions. The extension does have some
impact on other parts of Java, especially Java arrays, and the Java class library. The paper also explains how to
implement the extensions. We first sketched two designs that do not change the JVM, but sacrifice some space
or time performance. Our implementation avoids these performance problems. We had three main goals: to
allow all instantiations to share the same bytecodes (avoiding code blow-up), to have good performance when
using parameterized code, and to have little impact on the performance of code that does not use
parameterization.
24

Bibliography
 ISC Computer Science with Java – A textbook of class XI By Sumitra Arora – Published by
Dhanpat Rai and Co.
 ICSE Computer Applications Class X – By T.D. Malhotra – Published by Evergreen
Publications.

You might also like