Posts

PACKAGES USE WITH SHAPES

//package for rectangle package com.ahmed; public class rectangle { public int area(int l,int b ){ return l*b; } public int volume (int l,int b,int h){ return l*b*h; } } //package for triangle package com.ahmed; public class triangle { public float area(float b ,float h){ float k= (b*h)/2; return k; } public float volume(int b , int h ){ return b*h; } } //package for square package com.ahmed; public class square { public int area(int s){ return s*s; } public int volume(int s){ return s*s*s; } } //implementation of packages which were made above import com.ahmed.rectangle; import java.util.Scanner; import com.ahmed.triangle; import com.ahmed.square; public class mainfile { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("1)rectagle \t2)triangle \t3)square\t 4)exit\n"); ...

Guess the number game using classes and methods

import java.util.Scanner; import java.util.Random; public class guessnum { public static void main(String[] args) { Game guessGame = new Game(); boolean b = false; while(!b){ guessGame.getUserinput(); b = guessGame.Correctnumber(); } } } class Game { int computer; int input ; int noofGuesses =0; Game(){ Random rand = new Random(); computer = rand.nextInt(100); } int getUserinput(){ Scanner sc = new Scanner(System.in); System.out.println("guess the number"); input = sc.nextInt(); return input ; } boolean Correctnumber(){ noofGuesses++; if (input == computer){ System.out.printf("you have guessed correct number %d in %d attempts ",computer,noofGuesses); return true; } else if (input computer){ ...