Posts

Showing posts from November, 2020

java (day 17)practical notes

  overriding package com.company ; class A { public int a ; public int harry (){ return 4 ; } public void meth2 (){ System . out .println( "I am method 2 of class A" ); } } class B extends A { @Override public void meth2 (){ System . out .println( "I am method 2 of class B" ); } public void meth3 (){ System . out .println( "I am method 3 of class B" ); } } public class cwh_chapter10_method_overriding { public static void main ( String [] args) { A a = new A(); a .meth2(); B b = new B(); b .meth2(); } } this-super keyword package com.company ; import javax.print.Doc ; class EkClass { int a ; public int getA () { return a ; } EkClass ( int a){ this . a = a; } public int returnone (){ return 1 ; } } class DoClass extends EkClass { DoClass ( int c){ super (c);

java(day16)

Image

java chapter 9 practice set

  package com.company ; class Cylinder { private int radius ; private int height ; public Cylinder ( int radius, int height) { this . radius = radius; this . height = height; } public int getRadius () { return radius ; } public void setRadius ( int radius) { this . radius = radius; } //constructor public int getHeight () { return height ; } public void setHeight ( int height) { this . height = height; } public double surfaceArea (){ return 2 * Math . PI * radius * radius + 2 * Math . PI * radius * height ; } public double volume (){ return Math . PI * radius * radius * height ; } } class Rectangle { private int length ; private int breadth ; public Rectangle () { //problem 4 this . length = 4 ; this . breadth = 5 ; } public Rectangle ( int length, int breadth) { this . length = length; this . breadth = breadt

java exercise 3

  guess the number /* Create a class Game, which allows a user to play "Guess the Number" game once. Game should have the following methods: 1. Constructor to generate the random number 2. takeUserInput() to take a user input of number 3. isCorrectNumber() to detect whether the number entered by the user is true 4. getter and setter for noOfGuesses Use properties such as noOfGuesses(int), etc to get this task done! */ package com.company ; import java.util.Random ; import java.util.Scanner ; class Game { int compNumber ; private int noOfGuesses = 0 ; Game (){ Random random = new Random(); compNumber = random .nextInt( 100 ); } /*void userInput(int n){ int userInput = n; } I can't find any use of this, so commented it. */ boolean isCorrect ( int n){ if ( compNumber == n){ return true ; } else

java (day 15)

Image

JAVA EXERCISE 2

  GAME ROCK PAPER SCISSORS: package com.company; import java.util.Random; import java.util.Scanner; public class cwh_41_ex2sol {     public static void main(String[] args) {         // 0 for Rock         // 1 for Paper         // 2 for Scissor         Scanner sc = new Scanner(System.in);         System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissor ");         int userInput = sc.nextInt();         Random random = new Random();         int computerInput = random.nextInt(3);         if (userInput == computerInput) {             System.out.println("Draw");         }         else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0                 || userInput == 2 && computerInput == 1) {             System.out.println("You Win!");         } else {             System.out.println("Computer Win!");         }         // System.out.println("Computer choice: " + computerInput);      

java practice set 8

  questions: /* 1. Create a class Employee with the following properties and methods: Salary (property) (int) getSalary (method returning int) name (property) (String) getName (method returning String) setName (method changing name) 2. Create a class cellphone with methods to print “ringing…”, “vibrating…”, etc. 3. Create a class Square with a method to initialize its side, calculating area, perimeter etc. 4. Create a class Rectangle & problem 3. 5. Create a class TommyVecetti for Rockstar Games capable of hitting (print hitting…), running, firing, etc. 6. Repeat problem 4 for a circle. */ answers package com.company; class Employee{ int salary; String name; public int getSalary(){ return salary; } public String getName(){ return name; } public void setName(String n){ name = n; } } class CellPhone{ public void ring(){ System.out.print

java(day-14)

  create own class: package com.company; class Employee{     int id;     int salary;     String name;     public void printDetails(){         System.out.println("My id is " + id);         System.out.println("and my name is "+ name);     }     public int getSalary(){         return salary;     } } public class cwh_38_custom_class {     public static void main(String[] args) {         System.out.println("This is our custom class");         Employee harry = new Employee(); // Instantiating a new Employee Object         Employee john = new Employee(); // Instantiating a new Employee Object         // Setting Attributes for Harry         harry.id = 12;         harry.salary = 34;         harry.name = "CodeWithHarry";         // Setting Attributes for John         john.id = 17;         john.salary = 12;         john.name = "John Khandelwal";         // Printing the Attributes         harry.printDetails();         john.printDetails();         i

java practice set 7

/* Java Tutorial: Practice Questions on Java Methods 1.       Write a Java method to print the multiplication table of a number n. 2.       Write a program using functions to print the following pattern:                *                **                ***                **** 3.       Write a recursive function to calculate the sum of first n natural numbers. 4.   Write a function to print the following pattern:              ****              ***              **              * 5.   Write a function to print the nth term of the Fibonacci series using recursion. 6.   Write a function to find the average of a set of numbers passed as arguments. 7.   Repeat problem 4 using Recursion. 8.   Repeat problem 2 using Recursion. 9.   Write a function to convert Celsius temperature into Fahrenheit. 10.   Repeat problem 3 using an iterative approach  */ package com.company; public class cwh_chapter7_practice_set {     //problem1   multiplication of n         static void multiplication(int n) {    

java practice set 6

/* 1.WHAT WILL BE OUTPUT OF THIS PROGRAM:         int a = 10;         if (a=11)             System.out.println(“I am 11”);         else             System.out.println(“I am not 11”); 2.   Write a program to find out whether a student is pass or fail; if it requires total 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user. 3. Calculate income tax paid by an employee to the government as per the slabs mentioned below:          Income Slab    Tax           2.5L – 5.0L  5%           5.0L – 10.0L 20%           Above 10.0L 30% Note that there is not tax below 2.5L. Take the input amount as input from the user. 4. Write a Java program to find out the day of the week given the number    [1 for Monday, 2 for Tuesday … and so on!] 5. Write a Java program to find whether a year entered by the user is a leap year or not. 6. Write a program to find out the type of website from the URL:       .com – commercial website       .org – organization

java practice question 5

/* 1.WHAT WILL BE OUTPUT OF THIS PROGRAM:         int a = 10;         if (a=11)             System.out.println(“I am 11”);         else             System.out.println(“I am not 11”); 2.   Write a program to find out whether a student is pass or fail; if it requires total 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user. 3. Calculate income tax paid by an employee to the government as per the slabs mentioned below:          Income Slab    Tax           2.5L – 5.0L  5%           5.0L – 10.0L 20%           Above 10.0L 30% Note that there is not tax below 2.5L. Take the input amount as input from the user. 4. Write a Java program to find out the day of the week given the number    [1 for Monday, 2 for Tuesday … and so on!] 5. Write a Java program to find whether a year entered by the user is a leap year or not. 6. Write a program to find out the type of website from the URL:       .com – commercial website       .org – organization

java practice set 4

/* 1.WHAT WILL BE OUTPUT OF THIS PROGRAM:         int a = 10;         if (a=11)             System.out.println(“I am 11”);         else             System.out.println(“I am not 11”); 2.   Write a program to find out whether a student is pass or fail; if it requires total 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user. 3. Calculate income tax paid by an employee to the government as per the slabs mentioned below:          Income Slab    Tax           2.5L – 5.0L  5%           5.0L – 10.0L 20%           Above 10.0L 30% Note that there is not tax below 2.5L. Take the input amount as input from the user. 4. Write a Java program to find out the day of the week given the number    [1 for Monday, 2 for Tuesday … and so on!] 5. Write a Java program to find whether a year entered by the user is a leap year or not. 6. Write a program to find out the type of website from the URL:       .com – commercial website       .org – organization