Posts

Showing posts from April, 2023

Practice for inheritance

Image
Practice for inheritance Create a class circle and use inheritance to create another class cylinder from it Create a class rectangle and use inheritance to create another class cuboid, try to keep it as close to the real-world scenario as possible (for your practice)  Create a method for area and volume in 1  create methods for area & volume in 2 and also create getters and setters  What is the order of constructor execution for the following inheritance hierarchy  Code: package Com.Suryawanshi ; class Circle{ int radius ; public int getRadius () { return radius ; } public void setRadius ( int radius) { this . radius = radius ; } Circle (){ System. out .println( "I am Non Parametrized Constructor" ) ; } Circle ( int r){ this . radius =r ; } public double circleArea (){ return Math. PI * this . radius * this . radius ; } } class Cylender extends Circle{ int height ; public ...

Guess the Number Game

Image
  Create a class Game, which allows a user to play the "Guess the Number" game once. The game should have the following methods: Constructor to generate the random number takeUserInput() to take a user input of a number isCorrectNumber() to detect whether the number entered by the user is true getter and setter for noOfGuesses Use properties such as noOfGuesses(int), etc to get this task done! Code: package Com.Suryawanshi ; import java.util.Random ; import java.util.Scanner ; class Game{ public int number ; public int inputNumber ; public int noOfGuesses ; public int getNoOfGuesses () { return noOfGuesses ; } public void setNoOfGuesses ( int noOfGuesses) { this . noOfGuesses = noOfGuesses ; } Game (){ Random rand = new Random() ; this . number = rand.nextInt( 100 ) ; } void takeUserInput (){ System. out .println( "Guess the Number" ) ; Scanner sc = new Scanner(System. in ) ; ...

Distance between two points using Point Class

Image
  The Point class represents a point with x and y coordinates. Create a class Point according to the class diagram given below: InputP1(x,y)=2,3 InputP2(x,y)=5,6 Output Should be: The distance of P1 from Origin is 3.605551275463989 The distance of P1 from P2 is 4.242640687119285 Code: package Com.Suryawanshi ; class Point{ private double x ; private double y ; public double getX () { return x ; } public void setX ( double x) { this . x = x ; } public double getY () { return y ; } public void setY ( double y) { this . y = y ; } public Point ( double x , double y) { this . x = x ; this . y = y ; } public double distance (){ return Math. sqrt (( this .getX())*( this .getX())+ ( this .getY())*( this .getY())) ; } public double distance (Point point){ return Math. sqrt (( this .getX()-point.getX())*( this .getX()-point.getX())+ ( thi...

Creating code for Rock Paper Scissors:

Image
package Com.Suryawanshi ; import java.util.Random ; import java.util.Scanner ; public class CWA_RockPaperScissor { public static void main (String[] args) { // 0 For Rock // 1 For Paper // 2 For Scissor Scanner sc = new Scanner(System. in ) ; System. out .println( "Enter 0 for Rock, 1 for Paper, 2 for Scissor " ) ; int userInput = sc.nextInt() ; System. out .println( "User Input " +userInput) ; 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" ) ; } ...

Practice Set For Basic OOPs Concept:

Image
  (Note: comments code and then run for each problem.) Problems- 1. Create a class Employee with the following properties and methods: Salary (property) (int) getSalary (returning int) name(property) (String) getName(Reurning String) setName(changing the name) 2. Create a class cellphone with methods to print ringing..., vibrating...., and calling friends....etc. 3. Create a class with a method to initiate its side, calculating area, perimeter, etc.  ------------------------------------------------------------------------------------------------------------------- Code: package Com.Suryawanshi ; 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 .println( "Ringing........" ...

Creating your own Java Class:

Image
p package Com.Suryawanshi ; class Employee{ int id ; String name ; int salary ; public void printDetails (){ System. out .println( "My id is " + id ) ; System. out .println( "My name is " + name ) ; } public int getSalary (){ return salary ; } } public class CreatingCustomClass { public static void main (String[] args) { Employee Abhay = new Employee() ; Abhay. id = 101 ; Abhay. name = "'Abhay Suryawanshi'" ; Abhay. salary = 15000 ; Abhay.printDetails() ; System. out .println( "My salary is " +Abhay.getSalary()) ; System. out .println( "------------------------------------------" ) ; Employee Sudhir = new Employee() ; Sudhir. id = 102 ; Sudhir. name = "'Sudhir Abhang'" ; Sudhir. salary = 16000 ; Sudhir.printDetails() ; System. out .println( "My sala...