0% found this document useful (0 votes)
4 views3 pages

Cenoura Mixo

The document contains Java code for a number guessing game, where players guess a randomly generated number within a limited number of attempts. It also includes class definitions for a Person, Calculator, Animal, Book, Car, and Rectangle, each with their respective methods and attributes. The code demonstrates basic object-oriented programming principles in Java.

Uploaded by

Suraba Compl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Cenoura Mixo

The document contains Java code for a number guessing game, where players guess a randomly generated number within a limited number of attempts. It also includes class definitions for a Person, Calculator, Animal, Book, Car, and Rectangle, each with their respective methods and attributes. The code demonstrates basic object-oriented programming principles in Java.

Uploaded by

Suraba Compl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Random;
import java.util.Scanner;
9999000

public class NumberGuessGame {


private int numberToGuess;
private int maxAttempts;
private int attempts;

public NumberGuessGame(int maxNumber, int maxAttempts) {


this.numberToGuess = new Random().nextInt(maxNumber) + 1;
this.maxAttempts = maxAttempts;
this.attempts = 0;
}

public void play() {


Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("Try to guess the number between 1 and " + numberToGuess
+ " within " + maxAttempts + " attempts.");

while (attempts < maxAttempts) {


System.out.print("Enter your guess: ");
int guess = scanner.nextInt();
attempts++;

if (guess == numberToGuess) {
System.out.println("Congratulations! You guessed the number in " +
attempts + " attempts.");
return;
} else if (guess < numberToGuess) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}

System.out.println("Game over! The correct number was: " + numberToGuess);


scanner.close();
}

public static void main(String[] args) {


NumberGuessGame game = new NumberGuessGame(100, 10);
game.play();
}
}

class Person {
private String name;
private int age;

public Person(String name, int age) {


this.name = name;
this.age = age;
}

public void introduce() {


System.out.println("Hi, my name is " + name + " and I am " + age + " years
old.");
}
}

class Calculator {
public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {


return a - b;
}
}

class Animal {
private String species;

public Animal(String species) {


this.species = species;
}

public void makeSound() {


System.out.println("The " + species + " makes a sound.");
}
}

class Book {
private String title;
private String author;

public Book(String title, String author) {


this.title = title;
this.author = author;
}

public void displayInfo() {


System.out.println("Title: " + title + ", Author: " + author);
}
}

class Car {
private String model;
private int year;

public Car(String model, int year) {


this.model = model;
this.year = year;
}

public void drive() {


System.out.println("Driving a " + year + " " + model);
}
}

class Rectangle {
private double width;
private double height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}

public double area() {


return width * height;
}
}

You might also like