Skip to main content

Posts

Showing posts with the label Java

Write a program to allow two players to play a game of dice. Each player has 5 consecutive Chances of rolling the dice. The player who has higher to total score is declared the winner. In case of a tie, the player who threw a lower number on the first roll is declared the winner

Write a program to allow two players to blay a game of dice. Each player has 5 consecutive Chances of rolling the dice. The player who has higher to tal score is declared the winner. In case of a tie, the player who threw a lower number on the first roll is declared the winner Code : import java.util.Scanner; public class Main {  public static void main(String[] args) {   Scanner sc = new Scanner(System.in);   int scoreA[] = new int[5];   int scoreB[] = new int[5];   int tscoreA = 0, tscoreB = 0;   System.out.println("Player 1 Plays:");   for (int i = 0; i < 5; i++) {    System.out.println("Press 1 to roll the dice");    sc.next();    int r = (int)(Math.random() * 6) + 1;    scoreA[i] = r;    tscoreA += r;   }   System.out.println("Player 2 Plays:");   for (int i = 0; i < 5; i++) {    System.out.println("Press 1 to roll the dice");    sc.next();    int ...

Write a program to accept a word. Check if the word is a happy word or not.

/** Suppose : VAT ASCII code of V : 86 -64 => 22 ASCII code of A : 65 - 64 => 1 ASCII code of T : 84 - 64 => 20 Position of V: 22 Position of A: 1 Position of T: 20 Subtract the ascii value with 64,so we get the position values To join the position values together, VAT => 22120 We need to perform this calculate 2*2+2*2+1*1+2*2+0*0 => 13 1*1+3*3 => 10 1*1+0*0 => 1 If we get 1 at the very end, then this is Happy Word */ import java.util.Scanner; class HappyWord {  public static void main(String args[])  {   Scanner sc = new Scanner(System.in);   System.out.println("Enter a word: ");   String str = sc.next().toUpperCase();   String num="";   for(int i=0;i<str.length();i++)   {    char ch = str.charAt(i); //we get each every character    int ascii = (int)ch;    int pos=ascii-64;    num=num+Integer.toString(pos);   }   System.out.println("The combined position is "+num);   i...

Write a program to accept a sentence. Display the word with maximum number of vowel.

Write a program to accept a sentence. Display the word with maximum number of vowel. import java.util.Scanner; class maxvowel {     public static void main(String args[])     {         Scanner sc=new Scanner(System.in);         System.out.println("Enter a sentence:");         String str=sc.nextLine()+" ";         String w="",mvw=""; //w word and mvw maximum vowel word          int mv=0; //maximum vowels         for(int i=0;i<str.length();i++)         {             char ch=str.charAt(i);             if(ch==' ')             {                 int nv=0; //number of vowels                  for(int j=0;j<w.length();j++)         ...

Write a program to accept a sentence. Find the shortest word present in the sentence.

Write a program to accept a sentence. Find the shortest word present in the sentence. import java.util.Scanner; class shortestWord {     public static void main(String args[])     {         Scanner sc=new Scanner(System.in);         System.out.println("Enter a sentence:");         String str=sc.nextLine()+" ";         int min=99999;         String w="",sw=""; //w for word          for(int i=0;i<str.length();i++)         {             char ch=str.charAt(i);             if(ch==' ')             {                 if(w.length()<min)                 {                     min=w.length();       ...

Write a program to accept 5 student names and their marks. Display the names in the descending order of the marks using bubble sort algorithm.

Write a program to accept 5 student names and their marks. Display the names in the descending order of the marks using bubble sort algorithm. Input: Ravi 25 Sheetal 30 Rajeev 27 Karan 28 Utsav 24 Output: Sheetal 30 Karan 28 Rajeev 27 Ravi 25 Utsav 24 import java.util.Scanner; class meritlist { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String names[] = new String[5]; int marks[] = new int[5]; System.out.println("Enter 5 names and their marks:"); for(int i=0;i<5;i++) { System.out.println("Enter name:"); names[i]=sc.next(); System.out.println("Enter marks:"); marks[i]=sc.nextInt(); } for(int i=0;i<5-1;i++) { for(int j=0;j<5-i-1;j++) { if(marks[j]<marks[j+1]) //for ascending order marks[j]>marks[j+1] { //marks sorting descending order  int t = marks[j]; marks[j=marks[j+1]; marks[j+1]=t; //name sortin...

Write a program to accept 5 names and their age. Arrange the names in ascending order along with their age.

Write a program to accept 5 names and their age. Arrange the names in ascending order along with their age. Input: Sheetal 25 Ishita 24 Punam 28 Avishek 20 Raima   22 Output: Avishek 20 Ishita  24 Punam 28 Raima 22 Sheetal 25 import java.util.Scanner; class sortnames { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter 5 names and their age:"); String names[] = new String[5]; int age[] = new int[5]; for(int i=0;i<5;i++) { System.out.println("Enter names:"); names[i]=sc.next(); System.out.println("Enter age:"); age[i]=sc.nextInt(); } for(int i=0;i<5-1;i++) { for(int j=0;j<5-i-1;j++) { if(names[j].compareTo(names[j+1])>0) //>0 means ascending and <0 means descending { //name sort  String t=names[j]; names[j]=names[j+1]; names[j+1]=t; //age sort int temp = age[j]; age[j]=age...

File Handling in Java ISC Class 11

Binary File For Creating/Storing Information into the file FileOutputStream fout = new FileOutputStream(new File("filename",true)); //true for append DataOutputStream ffout = new DataOutputStream(fout); Functions to store data: Integers -  ffout.writeInt(); Floating -  ffout.writeFloat(); Double   -  ffout.writeDouble(); char     - ffout.writeChar(); String   - ffout.writeUTF(); For reading the file  FileInputStream fin = new FileInputStream(new File("filename")); DataInputStream ffin = new DataInputStream(fin); Functions to read data: Integers -  ffin.readInt(); Floating -  ffin.readFloat(); Double   -  ffin.readDouble(); char   ...

Math Library All Programs Solved Class 9 Java

[All Math library Programs From APC Class 9] import java.util.Scanner; class Q1{     public static void main(String args[]){         Scanner sc=new Scanner(System.in);         System.out.println("Enter three numbers:");         int a = sc.nextInt();         int b = sc.nextInt();         int c = sc.nextInt();         int g = Math.max(a,Math.max(b,c)); //greatest         int s = Math.min(a,Math.min(b,c)); //smallest         System.out.println("The greatest is:"+g);         System.out.println("The smallest is:"+s);     } } import java.util.Scanner; class Q2{     public static void main(String args[]){         Scanner sc=new Scanner(System.in);         System.out.println("Enter the perpendicular:");         int p = sc.nextInt...

Propositional Logic Notes Class 11 ISC

The concept of proposition means any statement which gives result either true or false. p: "Today is cold day" so p is the propositional constant and the rest is the statement.  There are two types of proposition: 1) Simple: Example is: p: "Today is holiday" q: " Dad is at home" 2)Complex/Compound: When we are combining multiple propositional statements into a single statement using logical connectives that is known as compound proposition. p and q: " Today is holiday and Dad is at home" over here"and" is the logical connective. There a multiple logical connectives such as: 1)Conjunction (and)  . / ^ 2)Disjunction (or) +/v 3) Negation (not) ~  4) Implication(if ....then) -> 5) Equivalence/Biconditional (if and only if) <=> or <-> 1) Conjunction   ------------------------ p: "Today is holiday" q: " Dad is at home" p^q: "Today is holiday and Dad is at home" Truth Table p.        q.    ...

ISC 2025 Theory Question: Colsum check

Design a class Colsum to check if the sum of elements in each corresponding column of two matrices is equal or not. Assume that the two matrices have the same dimensions. Example: INPUT: 2 3 1 7 5 6 1 4 2 MATRIX A 7 4 2 1 3 1 2 5 6 MATRIX B OUTPUT: Sum of corresponding columns is equal. The details of the members of the class are given below: Class name: Colsum Data members/instance variables: mat[][]: to store the integer array elements m: to store the number of rows n: to store the number of columns Member functions/methods: Colsum(int mm, int nn): parameterized constructor to initialize the data members m = mm and n = nn void readArray(): to accept the elements into the array boolean check(Colsum A, Colsum B): to check if the sum of elements in each column of the objects A and B is equal and return true, otherwise return false void print(): to display the array elements Specify the class Colsum giving details of the constructor, void readArray(), boolean check(Colsum, Colsum), and v...

ISC 2025 Theory Question Pernicious number

A class Perni has been defined to accept a positive integer in binary number system from the user and display if it is a Pernicious number or not. A pernicious number is a binary number that has minimum of two digits and has prime number of 1s in it. Examples: 101 is a pernicious number as the number of 1s in 101 = 2 and 2 is prime number. 10110 is a pernicious number as the number of 1s in 10110 = 3 and 3 is prime number. 1111 is NOT a pernicious number as the number of 1s in 1111 = 4 and 4 is NOT a prime number. The details of the members of the class are given below: Class name: Perni Data members/instance variables: num: to store a binary number Methods/Member functions: Perni(): constructor to initialize the data member with 0 void accept(): to accept a binary number (containing 0s and 1s only) int countOne(int k): to count and return the number of 1s in ‘k’ using recursive technique void check(): to check whether the given number is a pernicious number by invoking the function co...

ISC Program: Highest and Lowest ASCII code in lowercase and uppercase characters

 Write a program in Java to enter the string. Count and display: The character with lowest ASCII code in lower case The character with highest ASCII code in lower case The character with lowest ASCII code in upper case The character with highest ASCII code in upper case Sample Output: The character with lowest ASCII code in lower case: a The character with highest ASCII code in lower case: y The character with lowest ASCII code in upper case: E The character with highest ASCII code in upper case: P Solution: import ISjava.util.Scanner; class lowhighascii {     public static void main(){         Scanner sc=new Scanner(System.in);         System.out.println("Enter a sentence:");         String s = sc.nextLine();         int p=0,q=999,r=0,t=999;         for(int i=0;i<s.length();i++)         {             char ch = s.charAt(i...

ISC Program: Write a program in Java to accept a string and display the new string after reversing the characters of each word.

Write a program in Java to accept a string and display the new string after reversing the characters of each word. Sample Input: Understanding Computer Science Sample output: gnidnatsrednU retupmoC ecneicS Solution: import java.util.Scanner; class reverseEachWord {     public static void main()     {         Scanner sc=new Scanner(System.in);         System.out.println("Enter a sentence:");         String s=sc.nextLine()+" ";         String w="";         String res=" ";         for(int i=0;i<s.length();i++)         {             char ch = s.charAt(i);             if(ch==' ')             {                 String m="";                 for(int j=w.length()-1;j...

Java Mathematical Library Notes

Mathematical Library ---------------------- The math library is present under the package java.lang. Not under the package java.math . 1) Math.sqrt() -> return type is double. It returns the square root of a number. (a) double x = Math.sqrt(4);  => 2.0 (b) double y = Math.sqrt(-4); => NaN (Not a number) (for any negative value the answer is always NaN) 2) Math.cbrt() -> return type is double. It returns the cube root of a number (a) double x = Math.cbrt(8); => 2.0 (b) double y = Math.cbrt(-125); => -5.0 3) Math.pow() -> return type is double  Syntax:  Math.pow(base,exponent) 5 to the power of 3 (here 5 is base and 3 is exponent) (a) double x = Math.pow(5,3); => 125.0 (b) double y = Math.pow(16,1/2); => Math.pow(16,0); => 1.0     here 1/2 is 0 (L<R,/,0 when both are integer) (c) double z = Math.pow(16,1.0/2); => Math.pow(16,0.5) => 4.0 (d) double a = Math.pow(-16,1.0/2); => NaN  (e) double b = Math.pow(64,1.0/3); =...

ISC Program: Predict day of the week from date

Algorithm : 1)Take the last two digits of the year. 2)Divide by 4, discarding any fraction. 3)Add the day of the month. 4)Add the month's key value: JFM AMJ JAS OND 144 025 036 146 5)Subtract 1 for January or February of a leap year. 6)For a Gregorian date, add 0 for 1900's, 6 for 2000's, 4 for 1700's, 2 for 1800's; for other years, add or subtract multiples of 400. 7)For a Julian date, add 1 for 1700's, and 1 for every additional century you go back. 8)Add the last two digits of the year. 9)Divide by 7 and take the remainder. Example : Let's take a date: 26/03/2027 Last two digit of the year = 27 Divide by 4 discard fraction = 27/4 = 6.75 = 6 Add day = 6 + 26 = 32 Month key = 4 + 32 = 36 Add year code = 36 + 6 = 42 Now add two digits of the first year = 42 + 27 = 69 Now get the remainder after dividing by 7 = 69%7=6 So 1 is Sunday so 6 is Friday So 27/03/2027 Program : import java.util.Scanner; public class daydate {     public static void main(String[] arg...

ISC Program : Position deletion in a sentence

  WAP to accept  a sentence which may be terminated by either , ; ? . or !(Any other character may be ignored). The words may be separated by more than one blank space and are in upper case. Perform the following tasks: (i) Accept the sentence and reduce all extra blank spaces between two words to a single blank space. (ii) Accept a word from the user which is a part of the sentence along with its position number and delete the word and display the distance. Example: Input: AS YOU SOW,SO SO YOU REAP. AS YOU SOW,SO SO YOU REAP Word To be deleted : SO Word position in the sentence: 4 Output: AS YOU SOW,SO YOU REAP import java.util.*; public class code {     public static void main( String [] args) {         Scanner sc= new Scanner(System.in);         System.out.println( "Enter a sentence:" );         String m = sc.nextLine();         char ch = m.charAt(m.length()- 1 );   ...

ISC 2021 Program: Fascinating Number

Write a Program in Java to input a number and check whether it is a Fascinating Number or not. Fascinating Numbers: Some numbers of 3 digits or more exhibit a very interesting property. The property is such that, when the number is multiplied by 2 and 3, and both these products are concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless of the number of zeroes. Let's understand the concept of Fascinating Number through the following example: Consider the number 192 192 x 1 = 192 192 x 2 = 384 192 x 3 = 576 Concatenating the results: 192 384 576 It could be observed that '192384576' consists of all digits from 1 to 9 exactly once. Hence, it could be concluded that 192 is a Fascinating Number. Some examples of fascinating Numbers are: 192, 219, 273, 327, 1902, 1920, 2019 etc. /**  * suppose 192  * 192x1=192  * 192x2=384  * 192x3=576  * 192384576  * no single digit should repeat */ import java.util.Scanner; class fasc...

Money Object Passing Program in Java

 /**    Money is an unit that has 2 parts, Rupees and Paise, where 100 Paise=1 Rupee. Design a  class named Money whose details are as follows: Class Name : Money Data member: int rs, ps  : integer to store the value of Rupees and paise. Member methods: Money(…..)     : parameterized constructor to initialize member data void fnShow()  : to show the member data as Money [Rs 819.75] Money fnAdd(Money m1, Money m2) : Add m1 and m2. Store the result in corresponding member data and return it. Specify the class Money, giving the details of the above member data and methods.  Also write the main() to create objects and call the other methods accordingly to  enable the task of the adding 2 units on Money.    */ import java.util.Scanner; public class Money {     int rs,ps;     Money(int rs,int ps)     {         this.rs=rs;         this.ps=ps;     } ...