0% found this document useful (0 votes)
41 views10 pages

Computer Science 2 Practical File

The document contains 5 code examples demonstrating different Java programming concepts: 1. Constructor chaining by calling parent constructors in child constructors. 2. Matrix multiplication by multiplying rows of the first matrix with columns of the second. 3. Reading bytes from a file using FileInputStream and printing the bytes. 4. Creating a simple calculator applet using a GridLayout with buttons for numbers and operations. 5. Printing a triangle pattern of stars using nested for loops.

Uploaded by

Mahesh Pandula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views10 pages

Computer Science 2 Practical File

The document contains 5 code examples demonstrating different Java programming concepts: 1. Constructor chaining by calling parent constructors in child constructors. 2. Matrix multiplication by multiplying rows of the first matrix with columns of the second. 3. Reading bytes from a file using FileInputStream and printing the bytes. 4. Creating a simple calculator applet using a GridLayout with buttons for numbers and operations. 5. Printing a triangle pattern of stars using nested for loops.

Uploaded by

Mahesh Pandula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1 . Write Java Program to Show Chaining of Constructors.

public class ChainingDemo {


//default constructor of the class
public ChainingDemo(){
System.out.println("Default constructor");
}
public ChainingDemo(String str){
this();
System.out.println("Parametrized constructor with single param");
}
public ChainingDemo(String str, int num){
//It will call the constructor with String argument
this("Hello");
System.out.println("Parametrized constructor with double args");
}
public ChainingDemo(int num1, int num2, int num3){
// It will call the constructor with (String, integer) arguments
this("Hello", 2);
System.out.println("Parametrized constructor with three args");
}
public static void main(String args[]){
//Creating an object using Constructor with 3 int arguments
ChainingDemo obj = new ChainingDemo(5,5,15);
}
}

2 . Write A Java Program For Matrix Multiplication.

import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows in A: ");
int rowsInA = s.nextInt();
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = s.nextInt();
System.out.print("Enter number of columns in B: ");
int columnsInB = s.nextInt();
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = s.nextInt();
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {

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


}
System.out.println();
}
}

public static int[][] multiply(int[][] a, int[][] b) {


int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
}

3. Write a Program to Read Bytes From a File.


// reading bytes from a file
import java.io.*;

class ReadBytes
{
public static void main(String args[])
{
FileInputStream infile=null;
int b;
try
{
infile=new FileInputStream(args[0]);
while((b=infile.read())!=-1)
{
System.out.println((char)b);
}
infile.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}

4. Write a Java Program to Create a Simple Calculator


Using Grid Layout Manager.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="Calculator" width=300 height=300>
</applet>
*/

public class Calculator extends Applet


implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");

div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);

public void actionPerformed(ActionEvent ae)


{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))

{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{

t1.setText("");
}
}
}

5.Write a Java Program to Obtain the Following Output.


*
* *
* * *
* * * *
* * * * *

class Pattern
{

public static void main(String[] args) {


for(int i=0;i<5;i++) {
for(int j=0;j<5-i;j++) {
System.out.print(" ");
}
for(int k=0;k<=i;k++) {
System.out.print("* ");
}
System.out.println();
}
}
}

You might also like