0% found this document useful (0 votes)
20 views1 page

Java Programming Basics: Key Concepts

The document contains multiple Java code examples demonstrating various programming concepts. These include prime number generation, matrix multiplication, character/word/line counting from a file, random number generation, character array operations, and string manipulations. Each example is structured with a main method and utilizes standard input/output for interaction.

Uploaded by

rsingam464
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)
20 views1 page

Java Programming Basics: Key Concepts

The document contains multiple Java code examples demonstrating various programming concepts. These include prime number generation, matrix multiplication, character/word/line counting from a file, random number generation, character array operations, and string manipulations. Each example is structured with a main method and utilizes standard input/output for interaction.

Uploaded by

rsingam464
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

1■■ Prime Numbers

import [Link].*; class Prime { public static void main(String[] args) { Scanner
sc = new Scanner([Link]); int n = [Link](); for (int i = 2; i <= n; i++) {
boolean prime = true; for (int j = 2; j <= i / 2; j++) if (i % j == 0) { prime =
false; break; } if (prime) [Link](i); } } }

2■■ Matrix Multiplication


import [Link].*; class MatrixMul { public static void main(String[] a) { Scanner
s = new Scanner([Link]); int r1=[Link](), c1=[Link](), r2=[Link](),
c2=[Link](); if (c1!=r2) return; int[][] m1=new int[r1][c1], m2=new int[r2][c2],
res=new int[r1][c2]; for(int i=0;i

3■■ Count Characters, Words, Lines


import [Link].*; class CountFile { public static void main(String[] args)throws
Exception { BufferedReader br=new BufferedReader(new FileReader("[Link]")); int
chars=0, words=0, lines=0; String str; while((str=[Link]())!=null){ lines++;
String[] w=[Link]().split("\\s+"); if(![Link]().isEmpty()){ words+=[Link];
for(String x:w) chars+=[Link](); } } [Link]("Chars:"+chars+"
Words:"+words+" Lines:"+lines); } }

4■■ Random Numbers


import [Link].*; class RandomNum { public static void main(String[] a){ Random
r=new Random(); for(int i=0;i<5;i++) [Link]([Link](100)); } }

5■■ String Manipulation (Character Array)


import [Link].*; class CharArrayOps { public static void main(String[] args){
Scanner s=new Scanner([Link]); char[] a=[Link]().toCharArray(); char[]
b=[Link]().toCharArray(); [Link]("Length:"+[Link]); int
p=[Link](); if(p>=0&&p;

6■■ String Operations (String Class)


import [Link].*; class StringOps { public static void main(String[] args){
Scanner sc=new Scanner([Link]); String s1=[Link](), s2=[Link]();
[Link]("Concat:"+s1+s2); String main=[Link](), sub=[Link]();
[Link]([Link](sub)?"Found":"Not Found"); String ex=[Link]();
int st=[Link](), en=[Link]();
[Link]("Substr:"+[Link](st,en)); } }

You might also like