Siti Sal
0
Q:

how to make a new class

class Number {
    	
    	int number;
    	
    	// square numbers
    	public boolean isSquare(){
    		
    		double squareRoot = Math.sqrt(number);
    		
    		
    		if(squareRoot == Math.floor(squareRoot))
    		{
    			return true;
    		} else {
    			return false;
    		}
    		
    	}
    	
    	// triangular numbers
    	public boolean isTriangular(){
    		
    		int x = 1;
    		
    		int triangularNumber = 1;
    		
    		while(triangularNumber < number){
    			
    			x++;
    			triangularNumber = triangularNumber + x;
    			
    		}
    		
    		if(triangularNumber == number){
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    
    // testing 
    Number myNumber = new Number();
    myNumber.number = 16;
    
    System.out.println(myNumber.isSquare());
1
#Use the class function and give the class a name
#next use the def __init__() to initilaize and give it some properties.
class Fruits():
  def __init__(self, name, colour, taste):
    self.name = name
    self.colour = colour
    self.taste = taste
#Now create an object by first calling the class
fruit1 = Fruits(apple, red, sweet)
print(fruit1.name)
#this will print the name which is apple
print(fruit1.colour)
#this will print the colour which is red
print(fruit1.taste)
#this will print the taste which is sweet
0

New to Communities?

Join the community