Doggo Generator

Today made a simple program to practice a bit of Java to make sure I still remember everything.

It may not be the neatest but it was a whole lot of fun!

First I made three classes: infoGatherer.java, Dog.java, and infoGenerator.java.

The Dog class just initialises three attributes of the class Dog and contains their getters – functions which return a value to whatever is asking for it.
package org.jennings.miriam;
public class Dog {
String breed;
int age;
String colour;
public static String getBreed(Dog dog)
{
return dog.breed;
}
public static String getColour(Dog dog)
{
return dog.colour;
}
public static int getAge(Dog dog)
{
return dog.age;
}

In infoGenerator, I decided to randomly generate values for the age, colour, and breed of the dog each time the program is run.

I set some array data – a list of values to select from – for colour and breed, and generated random numbers as indexes for these arrays to select values and for an age between 0 and 30.
I then used setters to set these generated values for the attributes seen in Dog.java.

In infoGatherer, I accessed the getters to retrieve the values held for the new dog generated, and outputted a greeting in the form:
String description = (age + " year old "+ colour + " " + breed);
System.out.println("Hi! I am a " + description + "!");

I then thought that I could do something more interesting with this description.
I wanted an image of the dog to go with the greeting, but just showing a picture of any dog wouldn’t do. These dogs were being randomly generated every time the program ran. I needed to search for a dog image matching the description every time a new description was generated.
I made another class ‘imageFinder.jpg’ and learnt how to use Jsoup – a Java library for connecting to webpages and retrieving information, and followed a tutorial at https://www.javacodeexamples.com/jsoup-download-images-from-webpage-example/815, adapting the code a little for my purposes.

This allowed me to save the first image which appeared in an image search for the description which had been generated.

Now I was obtaining a dog image saved to my laptop every time I ran my program – definitely not something I would complain about! However, I only needed one image at a time so I made the program give the image the same name every time – overwriting the saved doggo.jpg.
I then inserted a few lines to open the file which had been saved…

Desktop desktop = Desktop.getDesktop();
File file = new File("C:/Users/miria/eclipse-workspace/Fun/DogImages/doggo.jpg");
if(file.exists()) desktop.open(file);

Now my program generates a description of a new dog and then shows an image of a dog for this description.

Leave a Reply

Your email address will not be published. Required fields are marked *